MoveObjectController.cs
4.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using UnityEngine;
using System.Collections;
public class MoveObjectController : MonoBehaviour
{
public float reachRange = 1.8f;
private Animator anim;
private Camera fpsCam;
private GameObject player;
private const string animBoolName = "isOpen_Obj_";
private bool playerEntered;
private bool showInteractMsg;
private GUIStyle guiStyle;
private string msg;
private int rayLayerMask;
void Start()
{
//Initialize moveDrawController if script is enabled.
player = GameObject.FindGameObjectWithTag("Player");
fpsCam = Camera.main;
if (fpsCam == null) //a reference to Camera is required for rayasts
{
Debug.LogError("A camera tagged 'MainCamera' is missing.");
}
//create AnimatorOverrideController to re-use animationController for sliding draws.
anim = GetComponent<Animator>();
anim.enabled = false; //disable animation states by default.
//the layer used to mask raycast for interactable objects only
LayerMask iRayLM = LayerMask.NameToLayer("InteractRaycast");
rayLayerMask = 1 << iRayLM.value;
//setup GUI style settings for user prompts
setupGui();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player) //player has collided with trigger
{
playerEntered = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player) //player has exited trigger
{
playerEntered = false;
//hide interact message as player may not have been looking at object when they left
showInteractMsg = false;
}
}
void Update()
{
if (playerEntered)
{
//center point of viewport in World space.
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f,0.5f,0f));
RaycastHit hit;
//if raycast hits a collider on the rayLayerMask
if (Physics.Raycast(rayOrigin,fpsCam.transform.forward, out hit,reachRange,rayLayerMask))
{
MoveableObject moveableObject = null;
//is the object of the collider player is looking at the same as me?
if (!isEqualToParent(hit.collider, out moveableObject))
{ //it's not so return;
return;
}
if (moveableObject != null) //hit object must have MoveableDraw script attached
{
showInteractMsg = true;
string animBoolNameNum = animBoolName + moveableObject.objectNumber.ToString();
bool isOpen = anim.GetBool(animBoolNameNum); //need current state for message.
msg = getGuiMsg(isOpen);
if (Input.GetKeyUp(KeyCode.E) || Input.GetButtonDown("Fire1"))
{
anim.enabled = true;
anim.SetBool(animBoolNameNum,!isOpen);
msg = getGuiMsg(!isOpen);
}
}
}
else
{
showInteractMsg = false;
}
}
}
//is current gameObject equal to the gameObject of other. check its parents
private bool isEqualToParent(Collider other, out MoveableObject draw)
{
draw = null;
bool rtnVal = false;
try
{
int maxWalk = 6;
draw = other.GetComponent<MoveableObject>();
GameObject currentGO = other.gameObject;
for(int i=0;i<maxWalk;i++)
{
if (currentGO.Equals(this.gameObject))
{
rtnVal = true;
if (draw== null) draw = currentGO.GetComponentInParent<MoveableObject>();
break; //exit loop early.
}
//not equal to if reached this far in loop. move to parent if exists.
if (currentGO.transform.parent != null) //is there a parent
{
currentGO = currentGO.transform.parent.gameObject;
}
}
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
return rtnVal;
}
#region GUI Config
//configure the style of the GUI
private void setupGui()
{
guiStyle = new GUIStyle();
guiStyle.fontSize = 16;
guiStyle.fontStyle = FontStyle.Bold;
guiStyle.normal.textColor = Color.white;
msg = "Press E/Fire1 to Open";
}
private string getGuiMsg(bool isOpen)
{
string rtnVal;
if (isOpen)
{
rtnVal = "Press E/Fire1 to Close";
}else
{
rtnVal = "Press E/Fire1 to Open";
}
return rtnVal;
}
void OnGUI()
{
if (showInteractMsg) //show on-screen prompts to user for guide.
{
GUI.Label(new Rect (50,Screen.height - 50,200,50), msg,guiStyle);
}
}
//End of GUI Config --------------
#endregion
}