-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModeRelocator.cs
More file actions
71 lines (58 loc) · 2.83 KB
/
ModeRelocator.cs
File metadata and controls
71 lines (58 loc) · 2.83 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DuskModules.ScriptableObjects {
/// <summary> Reads from an Int variable to determine what mode it is in, and moves itself to match target. </summary>
public class ModeRelocator : MonoBehaviour {
/// <summary> List of targets </summary>
[Tooltip("The list of targets this relocator can move from and to.")]
public List<Transform> targets;
/// <summary> Current mode to use </summary>
[Tooltip("The int value to use. Preferred to reference an IntVariable ScriptableObject.")]
public IntReference mode;
/// <summary> Speed to move position with. </summary>
[Tooltip("The speed to use to move the position with.")]
public LerpMoveValue positionSpeed;
/// <summary> Speed to move rotation with. </summary>
[Tooltip("The speed to use to move the rotation with.")]
public LerpMoveValue rotationSpeed;
/// <summary> Speed to move scale with. </summary>
[Tooltip("The speed to use to move the scale with.")]
public LerpMoveValue scaleSpeed;
/// <summary> Whether it should ignore out of bounds indexes and keep last position </summary>
[Tooltip("If true, any value falling outside of targets array bounds will be ignored. " +
"Use this to keep the last position for invalid indexes instead of clamping and taking the closest possible target index.")]
public bool ignoreOutOfBounds;
/// <summary> Value the mode must be for it to match with target index 0. </summary>
[Tooltip("The first valid value that can be used. The mode value set here will match with target index 0.")]
public int modeStartValue;
/// <summary> The mode to use </summary>
protected int useMode;
// Awaken and set to target immediatly.
protected virtual void Awake() {
useMode = Mathf.Clamp(mode.value, 0, targets.Count - 1);
Transform target = targets[useMode];
transform.localPosition = target.localPosition;
transform.localRotation = target.localRotation;
transform.localScale = target.localScale;
}
// Move self to target mode
protected virtual void Update() {
int modeValue = mode.value - modeStartValue;
if (!ignoreOutOfBounds)
useMode = Mathf.Clamp(modeValue, 0, targets.Count - 1);
else if (modeValue >= 0 && modeValue < targets.Count)
useMode = modeValue;
Transform target = targets[useMode];
Vector3 targetPos = target.localPosition;
Quaternion targetRot = target.localRotation;
Vector3 targetScale = target.localScale;
if (transform.localPosition != targetPos)
transform.localPosition = positionSpeed.Move(transform.localPosition, targetPos);
if (transform.localRotation != targetRot)
transform.localRotation = rotationSpeed.Move(transform.localRotation, targetRot);
if (transform.localScale != targetScale)
transform.localScale = scaleSpeed.Move(transform.localScale, targetScale);
}
}
}