using DG.Tweening; using UnityEngine; using UnityEngine.XR.Interaction.Toolkit; public class Printer3DInputHole : MonoBehaviour { public delegate void OnPlayerInsertedPrintableDelegate(); public event OnPlayerInsertedPrintableDelegate OnPlayerInsertedPrintable; 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(); if (printable != null) { if (printer.IsPrinting()) { Debug.Log("priner is busy!"); } else { ReleaseFromPlayer(printable); InsertPrintable(printable); OnPlayerInsertedPrintable?.Invoke(); } } } } private void ReleaseFromPlayer(Printable printable) { var grab = printable.GetComponent(); var rb = printable.GetComponent(); // 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; } // 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); }); } }