-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLookAtAction.cs
296 lines (258 loc) · 11.3 KB
/
LookAtAction.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
using Unity.LEGO.Utilities;
using UnityEngine;
namespace Unity.LEGO.Behaviours.Actions
{
public class LookAtAction : MovementAction
{
public enum LookAt
{
Player,
Transform
}
public enum Rotate
{
Horizontally,
Vertically,
Freely
}
[SerializeField, Tooltip("Look at the player.\nor\nLook at a transform.")]
LookAt m_LookAt = LookAt.Player;
[SerializeField, Tooltip("The transform to look at.")]
Transform m_TransformModeTransform = null;
[SerializeField, Tooltip("The angular speed in degrees per second.")]
int m_Speed = 180;
[SerializeField, Tooltip("Rotate horizontally only.\nor\nRotate vertically only.\nor\nRotate freely.")]
Rotate m_Rotate = Rotate.Horizontally;
enum State
{
Looking,
WaitingToLook
}
State m_State;
Transform m_PlayerTransform;
bool m_Initialised;
float m_RotationAngle;
Vector3 m_rotationAxis;
float m_VerticalRotatedAngle;
float m_HorizontalRotatedAngle;
public float GetVerticalRotatedAngle()
{
return m_VerticalRotatedAngle;
}
public float GetHorizontalRotatedAngle()
{
return m_HorizontalRotatedAngle;
}
protected override void Reset()
{
base.Reset();
m_Time = 1.0f;
m_IconPath = "Assets/LEGO/Gizmos/LEGO Behaviour Icons/Look At Action.png";
}
protected override void OnValidate()
{
base.OnValidate();
m_Speed = Mathf.Max(1, m_Speed);
m_Time = Mathf.Max(0.1f, m_Time);
}
void FixedUpdate()
{
if (m_Active)
{
if (!m_Initialised)
{
// Find player transform.
var player = GameObject.FindGameObjectWithTag("Player");
if (player)
{
m_PlayerTransform = player.transform;
}
m_Initialised = true;
}
// Update time.
m_CurrentTime += Time.fixedDeltaTime;
// Look.
if (m_State == State.Looking)
{
// Compute the rotation to look at the target.
ComputeRotation(out m_RotationAngle, out m_rotationAxis);
// Handle collision.
if (IsColliding())
{
m_CurrentTime = 0.0f;
m_VerticalRotatedAngle = 0.0f;
m_HorizontalRotatedAngle = 0.0f;
m_State = State.WaitingToLook;
}
else
{
// Play audio.
if (m_PlayAudio && m_RotationAngle > 0.1f)
{
PlayAudio();
m_PlayAudio = false;
}
// Rotate bricks.
var worldPivot = transform.position + transform.TransformVector(m_BrickPivotOffset);
m_Group.transform.RotateAround(worldPivot, m_rotationAxis, m_RotationAngle);
// Update model position.
m_MovementTracker.UpdateModelPosition();
// Find rotated angles. Used by custom editor to draw scene UI.
var rotation = Quaternion.AngleAxis(m_RotationAngle, m_rotationAxis);
switch (m_Rotate)
{
case Rotate.Horizontally:
{
UpdateHorizontalRotatedAngle(rotation);
break;
}
case Rotate.Vertically:
{
UpdateVerticalRotatedAngle(rotation);
break;
}
case Rotate.Freely:
{
UpdateHorizontalRotatedAngle(rotation);
UpdateVerticalRotatedAngle(rotation);
break;
}
}
// Check if we are done rotating.
if (m_CurrentTime >= m_Time)
{
m_CurrentTime = 0.0f;
m_VerticalRotatedAngle = 0.0f;
m_HorizontalRotatedAngle = 0.0f;
m_State = State.WaitingToLook;
}
}
}
// Waiting to look.
if (m_State == State.WaitingToLook)
{
if (m_CurrentTime >= m_Pause)
{
m_CurrentTime = 0.0f;
m_State = State.Looking;
m_PlayAudio = true;
m_Active = m_Repeat;
}
}
}
}
void UpdateHorizontalRotatedAngle(Quaternion rotation)
{
// Project both before and after unto XZ plane.
Vector3 before = Vector3.forward;
var after = rotation * before;
after.y = 0.0f;
m_HorizontalRotatedAngle += Vector3.SignedAngle(before, after, Vector3.up);
}
void UpdateVerticalRotatedAngle(Quaternion rotation)
{
// Project both before and after unto plane defined by current forward and world up.
var planeNormal = Vector3.Cross(transform.forward, Vector3.up);
Vector3 before = Vector3.ProjectOnPlane(Vector3.up, planeNormal);
var after = Vector3.ProjectOnPlane(rotation * before, planeNormal);
m_VerticalRotatedAngle += Vector3.SignedAngle(before, after, planeNormal);
}
void ComputeRotation(out float rotationAngle, out Vector3 rotationAxis)
{
Vector3 targetedPosition;
if (m_LookAt == LookAt.Player)
{
if (m_PlayerTransform)
{
targetedPosition = m_PlayerTransform.position + Vector3.up * 2.0f; // Offset by half player height.
}
else
{
targetedPosition = transform.position + transform.TransformVector(m_BrickPivotOffset) + transform.forward;
}
}
else
{
if (m_TransformModeTransform)
{
targetedPosition = m_TransformModeTransform.position;
}
else
{
targetedPosition = transform.position + transform.TransformVector(m_BrickPivotOffset) + transform.forward;
}
}
var desiredTargetDirection = targetedPosition - (transform.position + transform.TransformVector(m_BrickPivotOffset));
var currentDirection = transform.forward;
Quaternion rotationDelta = Quaternion.identity;
switch (m_Rotate)
{
case Rotate.Horizontally:
{
rotationDelta = MathUtility.ComputeHorizontalRotationDelta(currentDirection, desiredTargetDirection, m_Speed);
break;
}
case Rotate.Vertically:
{
rotationDelta = MathUtility.ComputeVerticalRotationDelta(currentDirection, desiredTargetDirection, m_Speed);
break;
}
case Rotate.Freely:
{
rotationDelta = MathUtility.ComputeHorizontalRotationDelta(currentDirection, desiredTargetDirection, m_Speed);
rotationDelta *= MathUtility.ComputeVerticalRotationDelta(currentDirection, desiredTargetDirection, m_Speed);
break;
}
}
rotationDelta.ToAngleAxis(out rotationAngle, out rotationAxis);
// Normalize rotation angle.
rotationAngle = Mathf.Min(m_Speed * Time.fixedDeltaTime, rotationAngle);
// Prevent overshoot.
rotationAngle = Mathf.Min(m_Speed * Mathf.Max(0.0f, m_Time - m_CurrentTime + Time.fixedDeltaTime), rotationAngle);
}
protected override bool IsColliding()
{
if (base.IsColliding())
{
foreach (var activeColliderPair in m_ActiveColliderPairs)
{
if (Physics.ComputePenetration(activeColliderPair.Item1, activeColliderPair.Item1.transform.position, activeColliderPair.Item1.transform.rotation,
activeColliderPair.Item2, activeColliderPair.Item2.transform.position, activeColliderPair.Item2.transform.rotation,
out Vector3 direction, out _))
{
// Attempt to find a point to represent the collision. This is an approximation.
Vector3 point;
var center = GetBrickCenter();
var colliderType = activeColliderPair.Item2.GetType();
if (colliderType == typeof(BoxCollider) || colliderType == typeof(SphereCollider) || colliderType == typeof(CapsuleCollider) || (colliderType == typeof(MeshCollider) && ((MeshCollider)activeColliderPair.Item2).convex))
{
point = activeColliderPair.Item2.ClosestPoint(center);
}
else
{
point = activeColliderPair.Item2.ClosestPointOnBounds(center);
}
point = activeColliderPair.Item1.ClosestPoint(point);
// Compute linear velocity of point as a result of the current rotation. This is again an approximation.
var rotatedPoint = Quaternion.AngleAxis(m_RotationAngle, m_rotationAxis) * (point - center) + center;
var velocity = rotatedPoint - point;
if (Vector3.Dot(direction, velocity) < -0.0001f)
{
return true;
}
}
}
}
return false;
}
protected override void OnDrawGizmos()
{
base.OnDrawGizmos();
if (m_LookAt == LookAt.Transform && !m_TransformModeTransform)
{
var gizmoBounds = GetGizmoBounds();
Gizmos.DrawIcon(gizmoBounds.center + Vector3.up, "Assets/LEGO/Gizmos/LEGO Behaviour Icons/Warning.png");
}
}
}
}