-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpivot_angle_script.cs
More file actions
140 lines (127 loc) · 4.35 KB
/
pivot_angle_script.cs
File metadata and controls
140 lines (127 loc) · 4.35 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
using UnityEngine;
using System.Collections;
/// <summary>
/// Manages the object's Z axis angle.
/// Stores the initial and last angles, provides methods to set and add angles.
/// </summary>
public class pivot_angle_script : MonoBehaviour
{
/// <summary>
/// The initial Z angle of the object.
/// </summary>
private float initialZ;
/// <summary>
/// The last set Z angle.
/// </summary>
private float lastZ;
/// <summary>
/// The current Z angle value, updated in the background for use by other scripts.
/// </summary>
[Tooltip("Current Z angle value (normalized, 0-360). Accessible by other scripts.")]
public float pivotZValue;
/// <summary>
/// Records the initial Z angle at start.
/// </summary>
void Start()
{
initialZ = 0f;
lastZ = initialZ;
SetZAngle(0f);
UpdatePivotZValue();
}
/// <summary>
/// Checks key input every frame.
/// Increases by 40 degrees with key 1, decreases by 40 degrees with key 2.
/// </summary>
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
SetZAngleSmooth(NormalizeAngle(GetLastZ() + 40f));
if (Input.GetKeyDown(KeyCode.Alpha2))
SetZAngleSmooth(NormalizeAngle(GetLastZ() - 40f));
UpdatePivotZValue();
}
/// <summary>
/// Sets the Z angle directly to the given value.
/// </summary>
/// <param name="z">New Z angle to set.</param>
public void SetZAngle(float z)
{
lastZ = NormalizeAngle(transform.eulerAngles.z);
float newZ = NormalizeAngle(z);
Vector3 angles = transform.eulerAngles;
angles.z = newZ;
transform.eulerAngles = angles;
UpdatePivotZValue();
}
/// <summary>
/// Adds the given amount to the Z angle.
/// </summary>
/// <param name="delta">Amount to add to the angle.</param>
public void AddZAngle(float delta)
{
lastZ = NormalizeAngle(transform.eulerAngles.z);
float newZ = NormalizeAngle(transform.eulerAngles.z + delta);
Vector3 angles = transform.eulerAngles;
angles.z = newZ;
transform.eulerAngles = angles;
UpdatePivotZValue();
}
/// <summary>
/// Sets the Z angle smoothly to the given value using a Coroutine.
/// </summary>
/// <param name="z">New Z angle to set.</param>
/// <param name="duration">Animation duration (seconds).</param>
public void SetZAngleSmooth(float z, float duration = 0.5f)
{
StopAllCoroutines();
StartCoroutine(SetZAngleCoroutine(z, duration));
}
private IEnumerator SetZAngleCoroutine(float targetZ, float duration)
{
float startZ = NormalizeAngle(transform.eulerAngles.z);
float elapsed = 0f;
lastZ = startZ;
targetZ = NormalizeAngle(targetZ);
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float newZ = Mathf.LerpAngle(startZ, targetZ, elapsed / duration);
newZ = NormalizeAngle(newZ);
Vector3 angles = transform.eulerAngles;
angles.z = newZ;
transform.eulerAngles = angles;
UpdatePivotZValue();
yield return null;
}
Vector3 finalAngles = transform.eulerAngles;
finalAngles.z = NormalizeAngle(targetZ);
transform.eulerAngles = finalAngles;
lastZ = finalAngles.z;
UpdatePivotZValue();
Debug.Log($"Pivot Z angle: {Mathf.RoundToInt(finalAngles.z)}");
}
/// <summary>
/// Returns the last set Z angle.
/// </summary>
public float GetLastZ() => lastZ;
/// <summary>
/// Normalizes the angle to the 0-360 range, rounds decimals, and sets very small values to 0.
/// </summary>
private float NormalizeAngle(float angle)
{
angle %= 360f;
if (angle < 0) angle += 360f;
angle = Mathf.Round(angle); // Remove decimals
if (Mathf.Abs(angle) < 0.01f || Mathf.Abs(angle - 360f) < 0.01f)
return 0f;
return angle;
}
/// <summary>
/// Updates the public Z angle value for use by other scripts.
/// </summary>
private void UpdatePivotZValue()
{
pivotZValue = NormalizeAngle(transform.eulerAngles.z);
}
}