-
Notifications
You must be signed in to change notification settings - Fork 0
/
gazeSystem.cs
127 lines (105 loc) · 3.79 KB
/
gazeSystem.cs
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
127
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gazeSystem : MonoBehaviour {
// declaring a variable for the reticle
public GameObject reticle;
// declaring reticles color variables
public Color inactiveReticleColor = Color.gray;
public Color activeReticleColor = Color.green;
/* declaring a private variable using a script as type.
this makes it possible to have access to all the function
used in the script serving as a type */
private gazableObjects currentGazeObject;
private gazableObjects currentSelectedObject;
// to answer a design question; this variable
private RaycastHit lastHit;
// Use this for initialization
void Start ()
{
// Debug.Log("started");
SetReticleColor(inactiveReticleColor);
//setInitialCameraPosition();
}
// Update is called once per frame
void Update () {
processGaze();
checkForInput(lastHit);
}
public void processGaze()
{
Ray raycastRay = new Ray(transform.position, direction: transform.forward);
RaycastHit hitInfo;
Debug.DrawRay(raycastRay.origin, raycastRay.direction * 100);
if (Physics.Raycast(raycastRay, out hitInfo))
{
// do something to the object
// get the gameObject from the hitInfo
GameObject hitObj = hitInfo.collider.gameObject;
// get the gazable object from the hit object
gazableObjects gazeObj = hitObj.GetComponent<gazableObjects>();
// to check if an object has a gazableObject
if (gazeObj != null)
{
// to check if the object we are looking at is different from the previous
if (gazeObj != currentGazeObject)
{
clearCurrentObject();
currentGazeObject = gazeObj;
currentGazeObject.OnGazeEnter(hitInfo);
SetReticleColor(activeReticleColor);
}
else
{
currentGazeObject.OnGaze(hitInfo);
}
}
else
{
clearCurrentObject();
}
lastHit = hitInfo;
}
else
{
clearCurrentObject();
}
}
public void SetReticleColor(Color reticleColor)
{
// to set reticle color
reticle.GetComponent<Renderer>().material.SetColor("_Color", reticleColor);
}
private void checkForInput(RaycastHit hitInfo)
{
if (Input.GetMouseButtonDown(0) && currentGazeObject != null)
{
// set currently selected object to current gaze object
currentSelectedObject = currentGazeObject;
// call the onPress method from gazableObjects script acting as a type
currentSelectedObject.OnPress(hitInfo);
}
// to check for hold
else if (Input.GetMouseButton(0) && currentGazeObject != null)
{
currentSelectedObject.OnHold(hitInfo);
}
// to check for release
else if (Input.GetMouseButtonUp(0) && currentGazeObject != null)
{
currentSelectedObject.OnRelease(hitInfo);
currentSelectedObject = null;
}
}
// when we look away form the game object. when not guessing
private void clearCurrentObject()
{
if (currentGazeObject != null)
{
currentGazeObject.OnGazeExit();
SetReticleColor(inactiveReticleColor);
// tell the system we are no longer looking at the game object
currentGazeObject = null;
}
}
}