@@ -10,11 +10,14 @@ public class LightSceneManager : MonoBehaviour
1010 public GameObject sceneContainer ;
1111 [ Tooltip ( "How long the pose must be held for before orb is spawned" ) ]
1212 public float poseHoldDuration = 0f ;
13+ [ Tooltip ( "How far the hand should be away from sphere to stop it following hand" ) ]
14+ public float handDistanceFromSphereStopTracking = 0.5f ;
1315 private GameObject currentOrbPrefab ;
1416 private bool leftHandPoseDetected = false ;
1517 private bool rightHandPoseDetected = false ;
1618 private MainSceneManager mainSceneManager ;
1719 private bool trackOrbToHands = false ;
20+ private MeshRenderer currentOrbMesh ;
1821
1922
2023 private void Start ( )
@@ -28,15 +31,63 @@ private void Update()
2831 {
2932 return ;
3033 }
31- Vector3 centerPoint = GetCenterPointBetweenHands ( ) ;
32- currentOrbPrefab . transform . position = centerPoint ;
34+ Vector3 ? centerPoint = GetCenterPointBetweenHands ( ) ;
35+ if ( centerPoint != null )
36+ {
37+
38+ currentOrbPrefab . transform . position = ( Vector3 ) centerPoint ;
39+ CheckIfHandsRemovedFromOrb ( ( Vector3 ) centerPoint ) ;
40+ }
41+
42+ }
43+
44+ /* When user moves their hands away from orb, stop tracking it with hands and leave in position */
45+ private void CheckIfHandsRemovedFromOrb ( Vector3 centerPoint )
46+ {
47+ if ( currentOrbMesh == null || centerPoint == null )
48+ {
49+ return ;
50+ }
51+ Hand leftHand = mainSceneManager . ultraleapService . GetHand ( Chirality . Left ) ;
52+ Hand rightHand = mainSceneManager . ultraleapService . GetHand ( Chirality . Right ) ;
53+ float leftHandDistanceFromOrbEdge = 0 ;
54+ float rightHandDistanceFromOrbEdge = 0 ;
55+
56+ if ( leftHand != null && leftHand . PalmPosition != null && centerPoint != null && currentOrbMesh != null )
57+ {
58+ leftHandDistanceFromOrbEdge = GetHandDistanceFromOrbEdge ( leftHand . PalmPosition , ( Vector3 ) centerPoint , currentOrbMesh ) ;
59+ if ( leftHandDistanceFromOrbEdge >= handDistanceFromSphereStopTracking )
60+ {
61+ OnPoseLost ( "left" ) ;
62+ }
63+ }
64+ if ( rightHand != null && rightHand . PalmPosition != null && centerPoint != null && currentOrbMesh != null )
65+ {
66+
67+ rightHandDistanceFromOrbEdge = GetHandDistanceFromOrbEdge ( rightHand . PalmPosition , ( Vector3 ) centerPoint , currentOrbMesh ) ;
68+ if ( rightHandDistanceFromOrbEdge >= handDistanceFromSphereStopTracking )
69+ {
70+ OnPoseLost ( "right" ) ;
71+ }
72+ }
73+ }
74+
75+ private float GetHandDistanceFromOrbEdge ( Vector3 handPosition , Vector3 centre , MeshRenderer mesh )
76+ {
77+ float distanceBetweenHandsAndOrbCentre = Vector3 . Distance ( handPosition , currentOrbPrefab . transform . position ) ;
78+ float orbRadius = currentOrbMesh . bounds . extents . x ;
79+ return distanceBetweenHandsAndOrbCentre - orbRadius ;
3380 }
3481
3582
36- private Vector3 GetCenterPointBetweenHands ( )
83+ private Vector3 ? GetCenterPointBetweenHands ( )
3784 {
3885 Hand leftHand = mainSceneManager . ultraleapService . GetHand ( Chirality . Left ) ;
3986 Hand rightHand = mainSceneManager . ultraleapService . GetHand ( Chirality . Right ) ;
87+ if ( leftHand == null || rightHand == null )
88+ {
89+ return null ;
90+ }
4091 Vector3 handCenterPoint = ( rightHand . PalmPosition + leftHand . PalmPosition ) / 2 ;
4192 return handCenterPoint ;
4293 }
@@ -46,7 +97,8 @@ public void OnPoseDetect(string hand)
4697 if ( hand == "left" )
4798 {
4899 leftHandPoseDetected = true ;
49- } else if ( hand == "right" )
100+ }
101+ else if ( hand == "right" )
50102 {
51103 rightHandPoseDetected = true ;
52104 }
@@ -61,6 +113,7 @@ private IEnumerator DebouncePoseDetect()
61113 {
62114 yield return new WaitForSeconds ( poseHoldDuration ) ;
63115 currentOrbPrefab = Instantiate ( orbPrefab , sceneContainer . transform ) ;
116+ currentOrbMesh = currentOrbPrefab . transform . GetChild ( 0 ) . GetChild ( 0 ) . GetComponent < MeshRenderer > ( ) ;
64117 trackOrbToHands = true ;
65118 }
66119
@@ -80,6 +133,7 @@ public void OnPoseLost(string hand)
80133 if ( ! leftHandPoseDetected || ! rightHandPoseDetected )
81134 {
82135 trackOrbToHands = false ;
136+ currentOrbMesh = null ;
83137 StopCoroutine ( DebouncePoseDetect ( ) ) ;
84138 }
85139 }
0 commit comments