ObjectController.cs
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//-----------------------------------------------------------------------
// <copyright file="ObjectController.cs" company="Google Inc.">
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
namespace GoogleVR.HelloVR
{
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>Controls interactable teleporting objects in the Demo scene.</summary>
[RequireComponent(typeof(Collider))]
public class ObjectController : MonoBehaviour
{
/// <summary>
/// The material to use when this object is inactive (not being gazed at).
/// </summary>
public Material inactiveMaterial;
/// <summary>The material to use when this object is active (gazed at).</summary>
public Material gazedAtMaterial;
private Vector3 startingPosition;
private Renderer myRenderer;
/// <summary>Sets this instance's GazedAt state.</summary>
/// <param name="gazedAt">
/// Value `true` if this object is being gazed at, `false` otherwise.
/// </param>
public void SetGazedAt(bool gazedAt)
{
if (inactiveMaterial != null && gazedAtMaterial != null)
{
myRenderer.material = gazedAt ? gazedAtMaterial : inactiveMaterial;
return;
}
}
/// <summary>Resets this instance and its siblings to their starting positions.</summary>
public void Reset()
{
int sibIdx = transform.GetSiblingIndex();
int numSibs = transform.parent.childCount;
for (int i = 0; i < numSibs; i++)
{
GameObject sib = transform.parent.GetChild(i).gameObject;
sib.transform.localPosition = startingPosition;
sib.SetActive(i == sibIdx);
}
}
/// <summary>Calls the Recenter event.</summary>
public void Recenter()
{
#if !UNITY_EDITOR
GvrCardboardHelpers.Recenter();
#else
if (GvrEditorEmulator.Instance != null)
{
GvrEditorEmulator.Instance.Recenter();
}
#endif // !UNITY_EDITOR
}
/// <summary>Teleport this instance randomly when triggered by a pointer click.</summary>
/// <param name="eventData">The pointer click event which triggered this call.</param>
public void TeleportRandomly(BaseEventData eventData)
{
// Only trigger on left input button, which maps to
// Daydream controller TouchPadButton and Trigger buttons.
PointerEventData ped = eventData as PointerEventData;
if (ped != null)
{
if (ped.button != PointerEventData.InputButton.Left)
{
return;
}
}
// Pick a random sibling, move them somewhere random, activate them,
// deactivate ourself.
int sibIdx = transform.GetSiblingIndex();
int numSibs = transform.parent.childCount;
sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs;
GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject;
// Move to random new location ±90˚ horzontal.
Vector3 direction = Quaternion.Euler(
0,
Random.Range(-90, 90),
0) * Vector3.forward;
// New location between 1.5m and 3.5m.
float distance = (2 * Random.value) + 1.5f;
Vector3 newPos = direction * distance;
// Limit vertical position to be fully in the room.
newPos.y = Mathf.Clamp(newPos.y, -1.2f, 4f);
randomSib.transform.localPosition = newPos;
randomSib.SetActive(true);
gameObject.SetActive(false);
SetGazedAt(false);
}
private void Start()
{
startingPosition = transform.localPosition;
myRenderer = GetComponent<Renderer>();
SetGazedAt(false);
}
}
}