-
Notifications
You must be signed in to change notification settings - Fork 5
/
Interactable.cs
297 lines (275 loc) · 10.5 KB
/
Interactable.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class Interactable : MonoBehaviour
{
public enum InteractableType { Move, Door, SetLight, SetLightNegative, Lever, Button, Item, UIButton, Trigger }
public InteractableType _Type;
private enum AxisOptions { x, y, z }
[SerializeField] private AxisOptions _AxisOption = AxisOptions.x;
[SerializeField] private bool _InvertMouse = false;
[Header("Type - Light")]
[SerializeField] private GameObject _Light = null;
[SerializeField] private bool _Light_StartOff = false;
[Header("Type - Lever/Door")]
[SerializeField] private Transform _LeverRotationPoint = null;
[SerializeField] private Vector2 _LeverMinMaxRotation = Vector2.zero;
[SerializeField] private float _CompleteDeathZone = 0;
[Header("Type - Button")]
[SerializeField] private float _ButtonPressDepth = 0;
private bool _ButtonPressed;
[Header("Type - Item")]
[SerializeField] private string _ItemInfo = "";
[Header("Speed")]
[SerializeField] private float _Speed = 1;
[Header("OnHigh")]
[SerializeField] private UnityEvent _OnHighEvent = null;
[Header("OnLow")]
[SerializeField] private UnityEvent _OnLowEvent = null;
[Header("OnNeutral")]
[SerializeField] private UnityEvent _OnNeutral = null;
[Header("Trigger")]
[SerializeField] private UnityEvent _OnTrigger = null;
private Vector3 velocity = Vector3.zero;
private Rigidbody _RB;
private Vector3 _DefaultLocalPosition;
private Vector3 _DefaultRotation;
private bool _MovingBack;
private void Start()
{
_DefaultLocalPosition = transform.localPosition;
_DefaultRotation = transform.eulerAngles;
_RB = GetComponent<Rigidbody>();
if (_Type == InteractableType.SetLight || _Type == InteractableType.SetLightNegative)
{
if (_Light_StartOff)
_Light.SetActive(false);
else
_Light.SetActive(true);
}
}
private void Update()
{
if (_Type == InteractableType.Button)
{
UpdateButton();
}
if (_MovingBack)
{
transform.eulerAngles = _DefaultRotation;
transform.localPosition = Vector3.MoveTowards(transform.localPosition, _DefaultLocalPosition, 10 * Time.deltaTime);
if (transform.localPosition == _DefaultLocalPosition)
_MovingBack = false;
}
}
public void TriggerEvent()
{
_OnTrigger.Invoke();
}
public InteractableType Type()
{
return _Type;
}
public void GotoPickupPoint(Transform point)
{
_RB.velocity = Vector3.zero;
transform.position = Vector3.SmoothDamp(transform.position, point.position, ref velocity, 0.2f);
transform.rotation = Quaternion.RotateTowards(transform.rotation, point.rotation, 5f);
}
public void SetVelocity(Vector3 velocity)
{
_RB.velocity = velocity;
}
public void TrowObject(Transform transformtrow)
{
_RB.AddForce(transformtrow.forward * 5000);
}
public void OpenDoor()
{
float mouseY = Input.GetAxis("Mouse Y");
float angle = 0;
switch (_AxisOption)
{
case AxisOptions.x:
angle = _LeverRotationPoint.localEulerAngles.x;
break;
case AxisOptions.y:
angle = _LeverRotationPoint.localEulerAngles.y;
break;
case AxisOptions.z:
angle = _LeverRotationPoint.localEulerAngles.z;
break;
}
angle = (angle > 180) ? angle - 360 : angle;
HandleRotation(_LeverRotationPoint, new Vector2(0, mouseY), _LeverMinMaxRotation, 1.2f, angle);
}
public void MoveLever()
{
float mouseY = Input.GetAxis("Mouse Y");
float angle = 0;
switch (_AxisOption)
{
case AxisOptions.x:
angle = _LeverRotationPoint.localEulerAngles.x;
break;
case AxisOptions.y:
angle = _LeverRotationPoint.localEulerAngles.y;
break;
case AxisOptions.z:
angle = _LeverRotationPoint.localEulerAngles.z;
break;
}
angle = (angle > 180) ? angle - 360 : angle;
HandleRotation(_LeverRotationPoint, new Vector2(0, mouseY), _LeverMinMaxRotation, 1.2f, angle);
//Check
if (angle < _LeverMinMaxRotation.x + _CompleteDeathZone)
{
_OnLowEvent.Invoke();
}
if (angle > _LeverMinMaxRotation.y - _CompleteDeathZone)
{
_OnHighEvent.Invoke();
}
if (angle > _LeverMinMaxRotation.x + _CompleteDeathZone && angle < _LeverMinMaxRotation.y - _CompleteDeathZone)
{
_OnNeutral.Invoke();
}
}
public void PressButton(bool option)
{
_ButtonPressed = true;
}
public void PressButtonNegative()
{
_ButtonPressed = !_ButtonPressed;
}
public void SetLight(bool option)
{
_Light.SetActive(option);
}
public void SetLightNegative()
{
if (_Light.activeSelf)
_Light.SetActive(false);
else
_Light.SetActive(true);
}
public void ReturnToDefaultPos()
{
_MovingBack = true;
}
public string GetItemInfo()
{
return _ItemInfo;
}
public void PressUIButton()
{
_OnTrigger.Invoke();
}
private void HandleRotation(Transform effectedtransform, Vector2 mousemovement, Vector2 minmaxangle, float speed, float angle)
{
if (_InvertMouse)
{
mousemovement.x = mousemovement.x * -2;
mousemovement.y = mousemovement.y * -2;
}
switch (_AxisOption)
{
case AxisOptions.x:
effectedtransform.localEulerAngles += new Vector3((mousemovement.x + mousemovement.y) * speed, 0, 0);
if (angle < minmaxangle.x)
effectedtransform.localEulerAngles = new Vector3(minmaxangle.x + 0.5f, 0, 0);
if (angle > minmaxangle.y)
effectedtransform.localEulerAngles = new Vector3(minmaxangle.y - 0.5f, 0, 0);
break;
case AxisOptions.y:
effectedtransform.localEulerAngles += new Vector3(0, (mousemovement.x + mousemovement.y) * speed, 0);
if (angle < minmaxangle.x)
effectedtransform.localEulerAngles = new Vector3(0, minmaxangle.x + 0.5f, 0);
if (angle > minmaxangle.y)
effectedtransform.localEulerAngles = new Vector3(0, minmaxangle.y - 0.5f, 0);
break;
case AxisOptions.z:
effectedtransform.localEulerAngles += new Vector3(0, 0, (mousemovement.x + mousemovement.y) * speed);
if (angle < minmaxangle.x)
effectedtransform.localEulerAngles = new Vector3(0, 0, minmaxangle.x + 0.5f);
if (angle > minmaxangle.y)
effectedtransform.localEulerAngles = new Vector3(0, 0, minmaxangle.y - 0.5f);
break;
}
}
private void UpdateButton()
{
switch (_AxisOption)
{
case AxisOptions.x:
if (_ButtonPressed)
{
if (transform.localPosition.x > _DefaultLocalPosition.x - _ButtonPressDepth)
transform.localPosition -= new Vector3(_Speed, 0, 0) * Time.deltaTime;
else
{
transform.localPosition = new Vector3(_DefaultLocalPosition.x - _ButtonPressDepth - 0.001f, transform.localPosition.y, transform.localPosition.z);
_OnLowEvent.Invoke();
}
}
else
{
if (transform.localPosition.x < _DefaultLocalPosition.x + _ButtonPressDepth)
transform.localPosition += new Vector3(_Speed, 0, 0) * Time.deltaTime;
else
{
transform.localPosition = new Vector3(_DefaultLocalPosition.x + _ButtonPressDepth, transform.localPosition.y, transform.localPosition.z);
_OnHighEvent.Invoke();
}
}
break;
case AxisOptions.y:
if (_ButtonPressed)
{
if (transform.localPosition.y > _DefaultLocalPosition.y - _ButtonPressDepth)
transform.localPosition -= new Vector3(0, _Speed, 0) * Time.deltaTime;
else
{
transform.localPosition = new Vector3(_DefaultLocalPosition.x, _DefaultLocalPosition.y - _ButtonPressDepth - 0.001f, _DefaultLocalPosition.z);
_OnLowEvent.Invoke();
}
}
else
{
if (transform.localPosition.y < _DefaultLocalPosition.y)
transform.localPosition += new Vector3(0, _Speed, 0) * Time.deltaTime;
else
{
transform.localPosition = _DefaultLocalPosition;
_OnHighEvent.Invoke();
}
}
break;
case AxisOptions.z:
if (_ButtonPressed)
{
if (transform.localPosition.z > _DefaultLocalPosition.z - _ButtonPressDepth)
transform.localPosition -= new Vector3(0, 0, _Speed) * Time.deltaTime;
else
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, _DefaultLocalPosition.z - _ButtonPressDepth - 0.001f);
_OnLowEvent.Invoke();
}
}
else
{
if (transform.localPosition.z < _DefaultLocalPosition.z + _ButtonPressDepth)
transform.localPosition += new Vector3(0, 0, _Speed) * Time.deltaTime;
else
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, _DefaultLocalPosition.z + _ButtonPressDepth);
_OnHighEvent.Invoke();
}
}
break;
}
}
}