-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProceduralAnimation.cs
255 lines (200 loc) · 8.79 KB
/
ProceduralAnimation.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
using System;
using System.Collections;
using UnityEditor;
using UnityEngine;
public class ProceduralAnimation : MonoBehaviour
{
[SerializeField] LayerMask groundLayer;
[SerializeField] Transform pelvisTransform;
[SerializeField] Transform[] footIKTransforms;
[Space(10)]
[SerializeField] float stepSize = 1.5f;
[SerializeField] float stepInterval = 1.5f;
[SerializeField] float stepSpeed = 4f;
[SerializeField] AnimationCurve footLiftingCurve;
[Space(10)]
[SerializeField] float pelvisHeight = 1.6f;
[SerializeField] float pelvisMovementSpeed = 8f;
[Range(0f, 1f)]
[SerializeField] float towardsTarget = 0.5f;
[Space(10)]
[SerializeField] float tiltAmount = 20f;
[SerializeField] float tiltSpeed = 5f;
[Space(10)]
[SerializeField] float breathSpeed = 0.5f;
[SerializeField] float breathMovement = 0.005f;
[Space(10)]
[SerializeField] bool debug = false;
private Vector3[] footLockedLocations;
private Vector3[] footTargetLocations;
private Vector3[] footLocalTransforms;
private Vector3 averageFootLocation;
private Vector3 previousWorldLocation;
private Vector3 smoothedVelocity = Vector3.zero;
private float[] footTimers;
private bool[] wantToMove;
private bool[] isMoving;
private int numLegs;
// Start is called before the first frame update
void Start()
{
numLegs = footIKTransforms.Length;
footLockedLocations = new Vector3[numLegs];
footTargetLocations = new Vector3[numLegs];
footLocalTransforms = new Vector3[numLegs];
footTimers = new float[numLegs];
wantToMove = new bool[numLegs];
isMoving = new bool[numLegs];
for (int i = 0; i < numLegs; i++)
{
footLockedLocations[i] = footIKTransforms[i].position;
footTargetLocations[i] = footIKTransforms[i].position;
footLocalTransforms[i] = footIKTransforms[i].position - transform.position;
footTimers[i] = (float)i / numLegs;
wantToMove[i] = false;
isMoving[i] = false;
}
previousWorldLocation = transform.position;
}
IEnumerator PerformStep(int i)
{
float lerpAlpha = 0f;
while (lerpAlpha < 1f)
{
footIKTransforms[i].position = Vector3.Lerp(footLockedLocations[i], footTargetLocations[i], lerpAlpha);
// make the leg move higher if the target is farther away
float distance = Vector3.Distance(footIKTransforms[i].position, footTargetLocations[i]);
footIKTransforms[i].position +=
footLiftingCurve.Evaluate(lerpAlpha) * // up and down curve
Remap(distance, 0, stepSize * 3, 0, 5) * Vector3.up;
lerpAlpha += Time.deltaTime * stepSpeed;
yield return null;
}
footIKTransforms[i].position = footTargetLocations[i];
footLockedLocations[i] = footTargetLocations[i];
wantToMove[i] = false;
isMoving[i] = false;
footTimers[i] = (float)i / numLegs;
}
void Update()
{
AdjustPelvisTransform();
CalculateVelocity();
CalculateNewFootTarget();
for (int i = 0; i < numLegs; i++)
{
// calculate the distance between foot's current location and the target location
float distance = Vector3.Distance(footLockedLocations[i], footTargetLocations[i]);
// if the distance is greater than the threshold, increase the timer to trigger the movement
if (distance > stepSize && wantToMove[i] == false)
{
// add a bit randomness so all legs won't be moving at the same time
footTimers[i] = stepInterval - Remap(i, 0, numLegs, 0f, 0.25f);
// set the flag to true so the next frame won't enter this branch
wantToMove[i] = true;
}
// time to move the leg
if (footTimers[i] >= stepInterval && !isMoving[i])
{
isMoving[i] = true;
StartCoroutine(PerformStep(i));
}
else
{
// otherwise stick to the current position
footIKTransforms[i].position = footLockedLocations[i];
}
footTimers[i] += Time.deltaTime;
}
CalculateAverageFootLocation();
}
private void AdjustPelvisTransform()
{
// lift the pelvis to avoid collision with the environment
if (Physics.SphereCast(averageFootLocation + Vector3.up * 5f, 2f, Vector3.down, out RaycastHit hitInfo, groundLayer))
{
pelvisTransform.position = Vector3.Lerp(pelvisTransform.position, hitInfo.point + Vector3.up * pelvisHeight, Time.deltaTime * pelvisMovementSpeed);
}
// tilt the pelvis towards the movement
float z = Vector3.Dot(transform.forward, smoothedVelocity.normalized) * smoothedVelocity.magnitude * tiltAmount;
float x = Vector3.Dot(transform.right, smoothedVelocity.normalized) * smoothedVelocity.magnitude * tiltAmount;
float y = transform.eulerAngles.y + 90;
Quaternion targetRotation = Quaternion.Euler(x, y, z);
pelvisTransform.rotation = Quaternion.Lerp(pelvisTransform.rotation, targetRotation, Time.deltaTime * tiltSpeed);
// simulate breathing by adding some vertical movements
pelvisTransform.position += breathMovement * Mathf.Sin(Time.time * breathSpeed) * Vector3.up;
}
private float Remap(float value, float from1, float to1, float from2, float to2)
{
// clamp the value
value = Math.Max(from1, value);
value = Math.Min(value, to1);
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
private Vector3 Remap(Vector3 value, float from1, float to1, float from2, float to2)
{
// clamp the value
value = Vector3.Max(Vector3.one * from1, value);
value = Vector3.Min(value, Vector3.one * to1);
return (value - Vector3.one * from1) / (to1 - from1) * (to2 - from2) + Vector3.one * from2;
}
private void CalculateAverageFootLocation()
{
Vector3 targetLocation = Vector3.zero;
Vector3 footLocation = Vector3.zero;
for (int i = 0; i < numLegs; i++)
{
targetLocation += footTargetLocations[i];
footLocation += footIKTransforms[i].position;
}
averageFootLocation = Vector3.Lerp(footLocation, targetLocation, towardsTarget) / numLegs;
}
private void CalculateNewFootTarget()
{
for (int i = 0; i < numLegs; i++)
{
// predicted foot position based on the current velocity
// smoothedVelocity would be clamped by the Remap function to prevent extreme values
Vector3 footDefaultPos = transform.TransformPoint(footLocalTransforms[i]);
Vector3 footPos = footDefaultPos + Remap(smoothedVelocity, -stepSize * 3, stepSize * 3, -stepSize, stepSize);
Vector3 centerPos = transform.position;
// offset towards centerPos to prevent leg collides with the environment
Vector3 start = Vector3.Lerp(footPos, centerPos, 0.5f) + Vector3.up * 5;
Vector3 end = footPos + Vector3.down * 5;
Vector3 direction = end - start;
if (Physics.SphereCast(start, 0.5f, direction, out RaycastHit hitInfo, 10, groundLayer))
{
footTargetLocations[i] = hitInfo.point;
}
else
{
footTargetLocations[i] = footPos;
}
}
}
private void CalculateVelocity()
{
Vector3 velocity = (transform.position - previousWorldLocation) / Time.deltaTime;
smoothedVelocity = Vector3.Lerp(smoothedVelocity, velocity, 5 * Time.deltaTime);
previousWorldLocation = transform.position;
}
public void OnDrawGizmos()
{
if (debug)
{
// draw foot locked and target locations
for (int i = 0; footTargetLocations != null && i < numLegs; i++)
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(footTargetLocations[i], 0.3f);
Gizmos.color = Color.red;
Gizmos.DrawSphere(footLockedLocations[i], 0.1f);
}
// draw velocity
Vector3 startPos = transform.position;
Vector3 endPos = startPos + smoothedVelocity;
float thickness = 10;
Handles.DrawBezier(startPos, endPos, startPos, endPos, Color.magenta, null, thickness);
}
}
}