forked from cgvr/DeltaVR
86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
|
|
public class Printer3DInputHole : MonoBehaviour
|
|
{
|
|
public Printer3D printer;
|
|
public XRInteractionManager interactionManager;
|
|
public Transform insertionOrigin;
|
|
public Transform insertionDestination;
|
|
public float insertionDuration = 1f;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
Debug.Log(other.gameObject.name);
|
|
Transform parent = other.transform.parent;
|
|
if (parent != null) {
|
|
Printable printable = parent.GetComponent<Printable>();
|
|
if (printable != null)
|
|
{
|
|
if (printer.IsPrinting())
|
|
{
|
|
Debug.Log("priner is busy!");
|
|
} else
|
|
{
|
|
ReleaseFromPlayer(printable);
|
|
InsertPrintable(printable);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReleaseFromPlayer(Printable printable)
|
|
{
|
|
|
|
var grab = printable.GetComponent<XRGrabInteractable>();
|
|
var rb = printable.GetComponent<Rigidbody>();
|
|
|
|
// 1) If currently held, force release from the interactor
|
|
if (grab != null && grab.isSelected)
|
|
{
|
|
var interactor = grab.firstInteractorSelecting; // the hand/controller currently holding it
|
|
if (interactor != null)
|
|
{
|
|
// Transfer ownership: tell the manager to stop the selection
|
|
interactionManager.SelectExit(interactor, grab);
|
|
}
|
|
}
|
|
|
|
// 2) Make sure physics doesn't fight the tween (optional but recommended)
|
|
if (rb != null)
|
|
{
|
|
rb.isKinematic = true; // prevent forces during tween
|
|
rb.velocity = Vector3.zero;
|
|
rb.angularVelocity = Vector3.zero;
|
|
}
|
|
|
|
// 3) Optionally disable grabbing during the tween so the player can't re-grab mid-flight
|
|
if (grab != null) grab.enabled = false;
|
|
}
|
|
|
|
private void InsertPrintable(Printable printable)
|
|
{
|
|
Transform printableTransform = printable.transform;
|
|
printableTransform.position = insertionOrigin.position;
|
|
printableTransform.rotation = insertionOrigin.rotation;
|
|
printableTransform.DOMove(insertionDestination.position, insertionDuration).OnComplete(() =>
|
|
{
|
|
printer.PrintObject(printable.GetTexture());
|
|
Destroy(printable.gameObject);
|
|
});
|
|
}
|
|
}
|