-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectController.cs
More file actions
123 lines (109 loc) · 4.23 KB
/
ObjectController.cs
File metadata and controls
123 lines (109 loc) · 4.23 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
//-----------------------------------------------------------------------
// <copyright file="ObjectController.cs" company="Google LLC">
// Copyright 2020 Google LLC
//
// 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>
//-----------------------------------------------------------------------
using System.Collections;
using UnityEngine;
/// <summary>
/// Controls target objects behaviour.
/// </summary>
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;
// The objects are about 1 meter in radius, so the min/max target distance are
// set so that the objects are always within the room (which is about 5 meters
// across).
private const float _minObjectDistance = 2.5f;
private const float _maxObjectDistance = 3.5f;
private const float _minObjectHeight = 0.5f;
private const float _maxObjectHeight = 3.5f;
private Renderer _myRenderer;
private Vector3 _startingPosition;
/// <summary>
/// Start is called before the first frame update.
/// </summary>
public void Start()
{
_startingPosition = transform.parent.localPosition;
_myRenderer = GetComponent<Renderer>();
SetMaterial(false);
}
/// <summary>
/// Teleports this instance randomly when triggered by a pointer click.
/// </summary>
public void TeleportRandomly()
{
// Picks a random sibling, activates it and deactivates itself.
int sibIdx = transform.GetSiblingIndex();
int numSibs = transform.parent.childCount;
sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs;
GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject;
// Computes new object's location.
float angle = Random.Range(-Mathf.PI, Mathf.PI);
float distance = Random.Range(_minObjectDistance, _maxObjectDistance);
float height = Random.Range(_minObjectHeight, _maxObjectHeight);
Vector3 newPos = new Vector3(Mathf.Cos(angle) * distance, height,
Mathf.Sin(angle) * distance);
// Moves the parent to the new position (siblings relative distance from their parent is 0).
transform.parent.localPosition = newPos;
randomSib.SetActive(true);
gameObject.SetActive(false);
SetMaterial(false);
}
/// <summary>
/// This method is called by the Main Camera when it starts gazing at this GameObject.
/// </summary>
public void OnPointerEnter()
{
SetMaterial(true);
}
/// <summary>
/// This method is called by the Main Camera when it stops gazing at this GameObject.
/// </summary>
public void OnPointerExit()
{
SetMaterial(false);
}
/// <summary>
/// This method is called by the Main Camera when it is gazing at this GameObject and the screen
/// is touched.
/// </summary>
public void OnPointerClick()
{
TeleportRandomly();
}
/// <summary>
/// Sets this instance's material according to gazedAt status.
/// </summary>
///
/// <param name="gazedAt">
/// Value `true` if this object is being gazed at, `false` otherwise.
/// </param>
private void SetMaterial(bool gazedAt)
{
if (InactiveMaterial != null && GazedAtMaterial != null)
{
_myRenderer.material = gazedAt ? GazedAtMaterial : InactiveMaterial;
}
}
}