From 8263edd59284aba390aca011d25b79efecef4c48 Mon Sep 17 00:00:00 2001 From: pryazha Date: Wed, 2 Jul 2025 08:46:23 -0700 Subject: init --- .../Scripts/ObjectResetPlane.cs | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Assets/Samples/XR Interaction Toolkit/3.1.2/Hands Interaction Demo/HandsDemoSceneAssets/Scripts/ObjectResetPlane.cs (limited to 'Assets/Samples/XR Interaction Toolkit/3.1.2/Hands Interaction Demo/HandsDemoSceneAssets/Scripts/ObjectResetPlane.cs') diff --git a/Assets/Samples/XR Interaction Toolkit/3.1.2/Hands Interaction Demo/HandsDemoSceneAssets/Scripts/ObjectResetPlane.cs b/Assets/Samples/XR Interaction Toolkit/3.1.2/Hands Interaction Demo/HandsDemoSceneAssets/Scripts/ObjectResetPlane.cs new file mode 100644 index 0000000..68f2817 --- /dev/null +++ b/Assets/Samples/XR Interaction Toolkit/3.1.2/Hands Interaction Demo/HandsDemoSceneAssets/Scripts/ObjectResetPlane.cs @@ -0,0 +1,96 @@ +using System.Collections; +using System.Collections.Generic; + +namespace UnityEngine.XR.Interaction.Toolkit.Samples.Hands +{ + /// + /// Provides the ability to reset specified objects if they fall below a certain position - designated by this transform's height. + /// + public class ObjectResetPlane : MonoBehaviour + { + [SerializeField] + [Tooltip("Which objects to reset if falling out of range.")] + List m_ObjectsToReset = new List(); + + [SerializeField] + [Tooltip("How often to check if objects should be reset.")] + float m_CheckDuration = 2f; + + [SerializeField] + [Tooltip("The object root used to compute local positions relative to. Objects will respawn relative to their position in this transform's hierarchy.")] + Transform m_ObjectRoot = null; + + readonly List m_OriginalPositions = new List(); + + float m_CheckTimer; + + /// + /// See . + /// + protected void Start() + { + foreach (var currentTransform in m_ObjectsToReset) + { + if (currentTransform != null) + { + var position = currentTransform.position; + + if (m_ObjectRoot != null) + position = m_ObjectRoot.InverseTransformPoint(currentTransform.position); + + m_OriginalPositions.Add(new Pose(position, currentTransform.rotation)); + } + else + { + Debug.LogWarning("Objects To Reset contained a null element. Update the reference or delete the array element of the missing object.", this); + m_OriginalPositions.Add(new Pose()); + } + } + } + + /// + /// See . + /// + protected void Update() + { + m_CheckTimer -= Time.deltaTime; + + if (m_CheckTimer > 0) + return; + + m_CheckTimer = m_CheckDuration; + + var resetPlane = transform.position.y; + + for (var transformIndex = 0; transformIndex < m_ObjectsToReset.Count; transformIndex++) + { + var currentTransform = m_ObjectsToReset[transformIndex]; + if (currentTransform == null) + continue; + + if (currentTransform.position.y < resetPlane) + { + var originalWorldPosition = m_OriginalPositions[transformIndex].position; + if (m_ObjectRoot != null) + originalWorldPosition = m_ObjectRoot.TransformPoint(originalWorldPosition); + + currentTransform.SetPositionAndRotation(originalWorldPosition, m_OriginalPositions[transformIndex].rotation); + + var rigidBody = currentTransform.GetComponentInChildren(); + if (rigidBody != null) + { + StartCoroutine(ResetRigidbodyRoutine(rigidBody)); + } + } + } + } + + IEnumerator ResetRigidbodyRoutine(Rigidbody body) + { + body.isKinematic = true; + yield return new WaitForFixedUpdate(); + body.isKinematic = false; + + } + } +} -- cgit v1.2.3-70-g09d2