-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformController.cs
More file actions
592 lines (508 loc) · 15.7 KB
/
Copy pathTransformController.cs
File metadata and controls
592 lines (508 loc) · 15.7 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright 2025 U.S. Federal Government (in countries where recognized)
// Copyright 2025 Dakota Crouchelli dakota.h.crouchelli.civ@us.navy.mil
using System;
using UnityEngine;
using UnityEngine.InputSystem;
namespace CREATIVE.Utility
{
/**
This class provides methods to modify and animate properties of a
GameObject's Transform.
*/
[RequireComponent(typeof(Transform))]
public class TransformController : MonoBehaviour
{
private enum Property { Position, Rotation }
private enum Dimension { X, Y, Z }
/**
Slowly start and slowly stop animations.
*/
[field: SerializeField]
private bool EaseInAndOutAnimation = true;
/**
How long (in seconds) animations will last.
*/
[field: SerializeField]
[Range(1, 10)]
private int AnimationDuration = 1;
/**
A reference to an Input action that will continuously affect a
property of this Transform.
*/
[field: SerializeField]
private InputActionReference DeltaInputAction;
private InputActionReference registeredDeltaInputAction;
/**
The property of this transform that an Input Action will
continuously affect.
*/
[field: SerializeField]
private Property DeltaInputProperty;
private Property registeredDeltaInputProperty;
/**
The dimension of the property in this transform that an Input Action
will continuously affect.
*/
[field: SerializeField]
private Dimension DeltaInputDimension;
private Dimension registeredDeltaInputDimension;
/**
Whether or not the Input action that will continuously affect a
property of this Transform will be started and later cancelled to
produce a continous effect, as opposed to rapidly performed.
*/
[field: SerializeField]
private bool DeltaInputWillBeHeld;
private bool registeredDeltaInputWillBeHeld;
/**
Whether or not the property of this Transform continuously affected
by an Input Action should stop being affected below a certain
minimum value.
*/
[field: SerializeField]
private bool DeltaInputHasMinimum = false;
private bool registeredDeltaInputHasMinimum;
/**
The minimum value at which the property of this Transform
continuously affected by an Input Action should stop decreasing.
*/
[field: SerializeField]
private float DeltaInputMinimum;
private float registeredDeltaInputMinimum;
/**
Whether or not the property of this Transform continuously affected
by an Input Action should stop being affected above a certain
maximum value.
*/
[field: SerializeField]
private bool DeltaInputHasMaximum = false;
private bool registeredDeltaInputHasMaximum;
/**
The maximum value at which the property of this Transform
continuously affected by an Input Action should stop increasing.
*/
[field: SerializeField]
private float DeltaInputMaximum;
private float registeredDeltaInputMaximum;
private float registeredDeltaInputCurrent;
private float registeredDeltaInputIncrement;
private InputAction registeredDeltaInputInProgress;
private bool registered = false;
private Vector3 initialPosition;
private Vector3 initialScale;
private Quaternion initialRotation;
private Animation animationInProgress = null;
void OnValidate() => ReRegister();
void OnEnable() => ReRegister();
void OnDisable() => UnRegister();
void OnDestroy() => UnRegister();
void Start()
{
initialPosition = transform.localPosition;
initialScale = transform.localScale;
initialRotation = transform.localRotation;
ReRegister();
}
/**
If an animation is in progress, run the next increment.
*/
void Update()
{
if (animationInProgress != null)
if (animationInProgress.Increment(Time.deltaTime))
animationInProgress = null;
if (registeredDeltaInputInProgress != null)
IncrementDeltaInput(true);
}
private void ReRegister()
{
UnRegister();
registeredDeltaInputAction = DeltaInputAction;
registeredDeltaInputProperty = DeltaInputProperty;
registeredDeltaInputDimension = DeltaInputDimension;
registeredDeltaInputWillBeHeld = DeltaInputWillBeHeld;
registeredDeltaInputHasMinimum = DeltaInputHasMinimum;
registeredDeltaInputHasMaximum = DeltaInputHasMaximum;
registeredDeltaInputMinimum = DeltaInputMinimum;
registeredDeltaInputMaximum = DeltaInputMaximum;
if (Application.isPlaying && isActiveAndEnabled && registeredDeltaInputAction!=null)
{
if
(
registeredDeltaInputHasMinimum && registeredDeltaInputHasMaximum &&
registeredDeltaInputMinimum >= registeredDeltaInputMaximum
)
Debug.LogError (nameof(DeltaInputMinimum) + " must be less than " + nameof(DeltaInputMaximum));
else
{
registeredDeltaInputCurrent = 0f;
registeredDeltaInputAction.action.started += ProcessDeltaInput;
registeredDeltaInputAction.action.canceled += ProcessDeltaInput;
registered = true;
}
}
}
private void UnRegister()
{
if (registered)
{
registeredDeltaInputAction.action.started -= ProcessDeltaInput;
registeredDeltaInputAction.action.canceled -= ProcessDeltaInput;
registered = false;
}
}
private void ProcessDeltaInput(InputAction.CallbackContext context)
{
if (context.phase==InputActionPhase.Canceled && registeredDeltaInputInProgress==context.action)
registeredDeltaInputInProgress = null;
else if (context.phase == InputActionPhase.Started)
{
registeredDeltaInputIncrement = context.ReadValue<float>();
if (registeredDeltaInputWillBeHeld)
registeredDeltaInputInProgress = context.action;
else
IncrementDeltaInput(false);
}
}
private void IncrementDeltaInput(bool scaleByTime)
{
if
(
Application.isPlaying && isActiveAndEnabled && registered &&
(
animationInProgress == null ||
registeredDeltaInputDimension != animationInProgress.Dimension ||
registeredDeltaInputProperty != animationInProgress.Property
)
)
{
float delta = registeredDeltaInputIncrement;
if (scaleByTime)
delta *= Time.deltaTime;
float current = 0f;
if (registeredDeltaInputProperty == Property.Position)
{
if (registeredDeltaInputDimension == Dimension.X) current = transform.localPosition.x;
if (registeredDeltaInputDimension == Dimension.Y) current = transform.localPosition.y;
if (registeredDeltaInputDimension == Dimension.Z) current = transform.localPosition.z;
}
if (registeredDeltaInputProperty == Property.Rotation)
current = registeredDeltaInputCurrent;
if
(
(
registeredDeltaInputProperty == Property.Position &&
(
!registeredDeltaInputHasMinimum ||
current > registeredDeltaInputMinimum ||
(current<=registeredDeltaInputMinimum && delta>0)
) &&
(
!registeredDeltaInputHasMaximum ||
current<registeredDeltaInputMaximum ||
(current>=registeredDeltaInputMaximum && delta<0)
)
) ||
(
registeredDeltaInputProperty == Property.Rotation &&
(
!registeredDeltaInputHasMinimum || !registeredDeltaInputHasMaximum ||
(
(
current > registeredDeltaInputMinimum ||
(current<=registeredDeltaInputMinimum && delta>0)
) &&
(
current<registeredDeltaInputMaximum ||
(current>=registeredDeltaInputMaximum && delta<0)
)
)
)
)
)
{
if (registeredDeltaInputProperty == Property.Rotation)
{
transform.Rotate(new Vector3
(
registeredDeltaInputDimension==Dimension.X? delta : 0,
registeredDeltaInputDimension==Dimension.Y? delta : 0,
registeredDeltaInputDimension==Dimension.Z? delta : 0
));
registeredDeltaInputCurrent += delta;
}
if (registeredDeltaInputProperty == Property.Position)
{
transform.Translate(new Vector3
(
registeredDeltaInputDimension==Dimension.X? delta : 0,
registeredDeltaInputDimension==Dimension.Y? delta : 0,
registeredDeltaInputDimension==Dimension.Z? delta : 0
));
}
}
}
}
/**
Sets the position of the object to its position at the start of the
scene (local space, not world space).
*/
public void SetInitialPosition()
{
if (animationInProgress==null || animationInProgress.Property != Property.Position)
transform.localPosition = initialPosition;
}
/**
Sets the scale of the object to its scale at the start of the scene
(local space, not world space).
*/
public void SetInitialScale() =>
transform.localScale = initialScale;
/**
Sets the rotation of the object to its rotation at the start of the
scene (local space, not world space).
*/
public void SetInitialRotation()
{
if (animationInProgress==null || animationInProgress.Property != Property.Rotation)
transform.localRotation = initialRotation;
}
/**
Sets the position of the object to the position of another Transform
(local space, not world space).
*/
public void SetPositionFromTransform(Transform reference)
{
if (animationInProgress==null || animationInProgress.Property != Property.Position)
transform.localPosition = reference.localPosition;
}
/**
Sets the scale of the object to the scale of another Transform
(local space, not world space).
*/
public void SetScaleFromTransform(Transform reference) =>
transform.localScale = reference.localScale;
/**
Sets the rotation of the object to the rotation of another Transform
(local space, not world space).
*/
public void SetRotationFromTransform(Transform reference)
{
if (animationInProgress==null || animationInProgress.Property != Property.Rotation)
transform.localRotation = reference.localRotation;
}
/**
Changes the parent of this GameObject while allowing the world
position to change. This this will preserve the local position of
the object.
*/
public void SetParentChangeWorldPosition(Transform newParent)
{
transform.SetParent(newParent, false);
}
/**
Start an animation to move the transform delta meters along the
X axis, positively or negatively.
*/
public void MoveX(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Position,
Dimension.X,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
/**
Start an animation to move the transform delta meters along the
Y axis, positively or negatively.
*/
public void MoveY(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Position,
Dimension.Y,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
/**
Start an animation to move the transform delta meters along the
Z axis, positively or negatively.
*/
public void MoveZ(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Position,
Dimension.Z,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
/**
Start an animation to rotate the transform delta degrees around the
X axis, positively or negatively.
*/
public void RotateX(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Rotation,
Dimension.X,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
/**
Start an animation to rotate the transform delta degrees around the
Y axis, positively or negatively.
*/
public void RotateY(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Rotation,
Dimension.Y,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
/**
Start an animation to rotate the transform delta degrees around the
Z axis, positively or negatively.
*/
public void RotateZ(float delta)
{
if (animationInProgress == null)
{
animationInProgress = new Animation
(
gameObject.transform,
Property.Rotation,
Dimension.Z,
delta,
AnimationDuration,
EaseInAndOutAnimation
);
}
}
// Internal class used to keep track of animation state.
private sealed class Animation
{
public readonly Transform Transform;
public readonly Property Property;
public readonly Dimension Dimension;
public readonly Vector3 VectorStart;
public readonly float PropertyValueEnd;
public readonly float Duration;
public readonly bool Ease;
private float TimeElapsed = 0f;
private bool AnimationComplete = false;
public float PropertyValueStart
{
get
{
if (Dimension == Dimension.X) return VectorStart.x;
if (Dimension == Dimension.Y) return VectorStart.y;
if (Dimension == Dimension.Z) return VectorStart.z;
return Mathf.NegativeInfinity;
}
}
public Animation
(
Transform transform,
Property property,
Dimension dimension,
float propertyValueDelta,
float duration,
bool ease
)
{
if (transform == null)
throw new ArgumentNullException("transform");
if (duration <= 0)
throw new InvalidOperationException("Duration must be greater than zero");
this.Transform = transform;
this.Property = property;
this.Dimension = dimension;
this.Duration = duration;
this.Ease = ease;
if (property == Property.Position) VectorStart = transform.localPosition;
if (property == Property.Rotation) VectorStart = transform.localEulerAngles;
if (dimension == Dimension.X) PropertyValueEnd = VectorStart.x + propertyValueDelta;
if (dimension == Dimension.Y) PropertyValueEnd = VectorStart.y + propertyValueDelta;
if (dimension == Dimension.Z) PropertyValueEnd = VectorStart.z + propertyValueDelta;
}
// Play one more step of the animation, based on how much time has
// passed since the last increment.
public bool Increment(float deltaTime)
{
if (AnimationComplete)
throw new InvalidOperationException("Animation is already complete");
if (deltaTime < 0)
throw new InvalidOperationException("Delta Time cannot be less than zero");
float propertyValue;
if (TimeElapsed + deltaTime >= Duration)
{
AnimationComplete = true;
TimeElapsed = Duration;
propertyValue = PropertyValueEnd;
}
else
{
TimeElapsed += deltaTime;
if (Ease)
propertyValue =
(
0.5f * (PropertyValueEnd - PropertyValueStart) *
(
- Mathf.Cos
(
TimeElapsed *
Mathf.PI /
Duration
) + 1
)
) + PropertyValueStart;
else
propertyValue = (PropertyValueEnd - PropertyValueStart) * (TimeElapsed / Duration);
}
Vector3 propertyVector = VectorStart;
if (Dimension == Dimension.X) propertyVector.x = propertyValue;
if (Dimension == Dimension.Y) propertyVector.y = propertyValue;
if (Dimension == Dimension.Z) propertyVector.z = propertyValue;
if (Property == Property.Position) Transform.localPosition = propertyVector;
if (Property == Property.Rotation) Transform.localEulerAngles = propertyVector;
return AnimationComplete;
}
}
}
}