|
4 | 4 | // you may not use this file except in compliance with the License. |
5 | 5 | // You may obtain a copy of the License at |
6 | 6 | // |
7 | | -// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | // |
9 | 9 | // Unless required by applicable law or agreed to in writing, software |
10 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -namespace GoogleVR.Hello6DoFController { |
16 | | - using UnityEngine; |
17 | | - using UnityEngine.EventSystems; |
18 | | - using System.Collections.Generic; |
19 | | - |
20 | | - public class DemoObjectController6DoF : MonoBehaviour { |
21 | | - private Vector3 startingPosition; |
22 | | - private Vector3 startingScale; |
23 | | - private bool isLockedToController; |
24 | | - private Renderer myRenderer; |
25 | | - |
26 | | - public Material inactiveMaterial; |
27 | | - public Material gazedAtMaterial; |
28 | | - |
29 | | - private GvrTrackedController grabController; |
30 | | - |
31 | | - void Start() { |
32 | | - startingPosition = transform.position; |
33 | | - startingScale = transform.localScale; |
34 | | - myRenderer = GetComponent<Renderer>(); |
35 | | - SetGazedAt(false); |
36 | | - } |
37 | | - |
38 | | - public void UpdateStartPosition() { |
39 | | - startingPosition = transform.position; |
40 | | - startingScale = transform.localScale; |
41 | | - } |
42 | | - |
43 | | - private void Update() { |
44 | | - Vector3 targetPos = startingPosition; |
45 | | - Quaternion targetRotation = Quaternion.identity; |
46 | | - Vector3 targetScale = startingScale; |
47 | | - |
48 | | - if (grabController != null) { |
49 | | - targetRotation = grabController.transform.rotation; |
50 | | - targetPos = grabController.transform.position; |
51 | | - // Offset the object 15cm down the pointing axis of the controller |
52 | | - // to place it in front of the controller. |
53 | | - targetPos += targetRotation * Vector3.forward * 0.20f; |
54 | | - // Shrink the object down a bit while "gripped". |
55 | | - targetScale *= 0.5f; |
56 | | - if (Vector3.Distance(targetPos, transform.position) < 0.01f) { |
57 | | - isLockedToController = true; |
| 15 | +namespace GoogleVR.Hello6DoFController |
| 16 | +{ |
| 17 | + using UnityEngine; |
| 18 | + using UnityEngine.EventSystems; |
| 19 | + using System.Collections.Generic; |
| 20 | + |
| 21 | + public class DemoObjectController6DoF : MonoBehaviour |
| 22 | + { |
| 23 | + private Vector3 startingPosition; |
| 24 | + private Vector3 startingScale; |
| 25 | + private bool isLockedToController; |
| 26 | + private Renderer myRenderer; |
| 27 | + |
| 28 | + public Material inactiveMaterial; |
| 29 | + public Material gazedAtMaterial; |
| 30 | + |
| 31 | + private GvrTrackedController grabController; |
| 32 | + |
| 33 | + void Start() |
| 34 | + { |
| 35 | + startingPosition = transform.position; |
| 36 | + startingScale = transform.localScale; |
| 37 | + myRenderer = GetComponent<Renderer>(); |
| 38 | + SetGazedAt(false); |
58 | 39 | } |
59 | | - } |
60 | 40 |
|
61 | | - float interpAmount = 1; |
62 | | - if (!isLockedToController) { |
63 | | - interpAmount = 1 - Mathf.Pow(0.01f, 4 * Time.deltaTime); |
64 | | - } |
| 41 | + public void UpdateStartPosition() |
| 42 | + { |
| 43 | + startingPosition = transform.position; |
| 44 | + startingScale = transform.localScale; |
| 45 | + } |
65 | 46 |
|
66 | | - transform.position = Vector3.Lerp(transform.position, targetPos, interpAmount); |
67 | | - transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, interpAmount); |
68 | | - transform.localScale = Vector3.Lerp(transform.localScale, targetScale, interpAmount); |
69 | | - } |
| 47 | + private void Update() |
| 48 | + { |
| 49 | + Vector3 targetPos = startingPosition; |
| 50 | + Quaternion targetRotation = Quaternion.identity; |
| 51 | + Vector3 targetScale = startingScale; |
| 52 | + |
| 53 | + if (grabController != null) |
| 54 | + { |
| 55 | + targetRotation = grabController.transform.rotation; |
| 56 | + targetPos = grabController.transform.position; |
| 57 | + |
| 58 | + // Offset the object 15cm down the pointing axis of the controller |
| 59 | + // to place it in front of the controller. |
| 60 | + targetPos += targetRotation * Vector3.forward * 0.20f; |
| 61 | + |
| 62 | + // Shrink the object down a bit while "gripped". |
| 63 | + targetScale *= 0.5f; |
| 64 | + if (Vector3.Distance(targetPos, transform.position) < 0.01f) |
| 65 | + { |
| 66 | + isLockedToController = true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + float interpAmount = 1; |
| 71 | + if (!isLockedToController) |
| 72 | + { |
| 73 | + interpAmount = 1 - Mathf.Pow(0.01f, 4 * Time.deltaTime); |
| 74 | + } |
| 75 | + |
| 76 | + transform.position = Vector3.Lerp(transform.position, targetPos, interpAmount); |
| 77 | + transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, interpAmount); |
| 78 | + transform.localScale = Vector3.Lerp(transform.localScale, targetScale, interpAmount); |
| 79 | + } |
70 | 80 |
|
71 | | - // Hook this up to Event Trigger PointerEnter with the checkbox checked |
72 | | - // and PointerExit with the checkbox unchecked. |
73 | | - public void SetGazedAt(bool gazedAt) { |
74 | | - if (inactiveMaterial != null && gazedAtMaterial != null) { |
75 | | - myRenderer.material = gazedAt ? gazedAtMaterial : inactiveMaterial; |
76 | | - } |
77 | | - } |
| 81 | + // Hook this up to Event Trigger PointerEnter with the checkbox checked |
| 82 | + // and PointerExit with the checkbox unchecked. |
| 83 | + public void SetGazedAt(bool gazedAt) |
| 84 | + { |
| 85 | + if (inactiveMaterial != null && gazedAtMaterial != null) |
| 86 | + { |
| 87 | + myRenderer.material = gazedAt ? gazedAtMaterial : inactiveMaterial; |
| 88 | + } |
| 89 | + } |
78 | 90 |
|
79 | | - // Hook this up to Event Trigger PointerDown. |
80 | | - public void GripStartGrab(BaseEventData eventData) { |
81 | | - PointerEventData ptrEventData = eventData as PointerEventData; |
82 | | - if (ptrEventData != null && |
83 | | - (ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0) |
84 | | - { |
85 | | - grabController = GvrPointerInputModule.Pointer. |
86 | | - GetComponentInParent<GvrTrackedController>(); |
87 | | - isLockedToController = false; |
88 | | - } |
89 | | - } |
| 91 | + // Hook this up to Event Trigger PointerDown. |
| 92 | + public void GripStartGrab(BaseEventData eventData) |
| 93 | + { |
| 94 | + PointerEventData ptrEventData = eventData as PointerEventData; |
| 95 | + if (ptrEventData != null && |
| 96 | + (ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0) |
| 97 | + { |
| 98 | + grabController = GvrPointerInputModule.Pointer.GetComponentInParent<GvrTrackedController>(); |
| 99 | + isLockedToController = false; |
| 100 | + } |
| 101 | + } |
90 | 102 |
|
91 | | - // Hook this up to Event Trigger PointerUp. |
92 | | - public void GripEndGrab(BaseEventData eventData) { |
93 | | - PointerEventData ptrEventData = eventData as PointerEventData; |
94 | | - if (ptrEventData != null && |
95 | | - (ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0) |
96 | | - { |
97 | | - grabController = null; |
98 | | - isLockedToController = false; |
99 | | - } |
100 | | - } |
| 103 | + // Hook this up to Event Trigger PointerUp. |
| 104 | + public void GripEndGrab(BaseEventData eventData) |
| 105 | + { |
| 106 | + PointerEventData ptrEventData = eventData as PointerEventData; |
| 107 | + if (ptrEventData != null && |
| 108 | + (ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0) |
| 109 | + { |
| 110 | + grabController = null; |
| 111 | + isLockedToController = false; |
| 112 | + } |
| 113 | + } |
101 | 114 |
|
102 | | - // Hook this up to Event Trigger PointerClick. |
103 | | - public void ClickTeleport(BaseEventData eventData) { |
104 | | - PointerEventData ptrEventData = eventData as PointerEventData; |
105 | | - if (ptrEventData != null && |
106 | | - ptrEventData.button == PointerEventData.InputButton.Left) { |
107 | | - TeleportRandomly(); |
108 | | - } |
109 | | - } |
| 115 | + // Hook this up to Event Trigger PointerClick. |
| 116 | + public void ClickTeleport(BaseEventData eventData) |
| 117 | + { |
| 118 | + PointerEventData ptrEventData = eventData as PointerEventData; |
| 119 | + if (ptrEventData != null && |
| 120 | + ptrEventData.button == PointerEventData.InputButton.Left) |
| 121 | + { |
| 122 | + TeleportRandomly(); |
| 123 | + } |
| 124 | + } |
110 | 125 |
|
111 | | - private void TeleportRandomly() { |
112 | | - // Pick a random sibling, move them somewhere random, activate them, |
113 | | - // deactivate ourself. |
114 | | - int sibIdx = transform.GetSiblingIndex(); |
115 | | - int numSibs = transform.parent.childCount; |
116 | | - sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs; |
117 | | - GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject; |
118 | | - |
119 | | - // Move to random new location ±90˚ horzontal. |
120 | | - Vector3 direction = Quaternion.Euler( |
121 | | - 0, |
122 | | - Random.Range(-90, 90), |
123 | | - 0) * Vector3.forward; |
124 | | - // New location between 1m and 2m. |
125 | | - float distance = Random.Range(1, 2); |
126 | | - Vector3 newPos = direction * distance; |
127 | | - // Limit vertical position to be fully in the room. |
128 | | - newPos.y = Mathf.Clamp(newPos.y, -1.2f, 4f); |
129 | | - randomSib.transform.localPosition = newPos; |
130 | | - randomSib.GetComponent<DemoObjectController6DoF>().UpdateStartPosition(); |
131 | | - randomSib.SetActive(true); |
132 | | - gameObject.SetActive(false); |
133 | | - SetGazedAt(false); |
| 126 | + private void TeleportRandomly() |
| 127 | + { |
| 128 | + // Pick a random sibling, move them somewhere random, activate them, |
| 129 | + // deactivate ourself. |
| 130 | + int sibIdx = transform.GetSiblingIndex(); |
| 131 | + int numSibs = transform.parent.childCount; |
| 132 | + sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs; |
| 133 | + GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject; |
| 134 | + |
| 135 | + // Move to random new location ±90˚ horzontal. |
| 136 | + Vector3 direction = Quaternion.Euler( |
| 137 | + 0, |
| 138 | + Random.Range(-90, 90), |
| 139 | + 0) * Vector3.forward; |
| 140 | + |
| 141 | + // New location between 1m and 2m. |
| 142 | + float distance = Random.Range(1, 2); |
| 143 | + Vector3 newPos = direction * distance; |
| 144 | + |
| 145 | + // Limit vertical position to be fully in the room. |
| 146 | + newPos.y = Mathf.Clamp(newPos.y, -1.2f, 4f); |
| 147 | + randomSib.transform.localPosition = newPos; |
| 148 | + randomSib.GetComponent<DemoObjectController6DoF>().UpdateStartPosition(); |
| 149 | + randomSib.SetActive(true); |
| 150 | + gameObject.SetActive(false); |
| 151 | + SetGazedAt(false); |
| 152 | + } |
134 | 153 | } |
135 | | - } |
136 | 154 | } |
0 commit comments