Skip to content

Commit e1ac7ca

Browse files
committed
Support different update modes for Orbit script
1 parent 46d8d75 commit e1ac7ca

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

Runtime/Orbit.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Zigurous.Animation
44
{
55
/// <summary>
6-
/// Rotates an object around another object with a given speed and radius.
6+
/// Orbits an object around another object with a given speed and radius.
77
/// </summary>
88
public sealed class Orbit : MonoBehaviour
99
{
1010
/// <summary>
11-
/// The transform the object rotates around.
11+
/// The transform the object orbits around.
1212
/// </summary>
13-
[Tooltip("The transform the object rotates around.")]
13+
[Tooltip("The transform the object orbits around.")]
1414
public Transform center;
1515

1616
/// <summary>
@@ -37,25 +37,52 @@ public sealed class Orbit : MonoBehaviour
3737
/// </summary>
3838
public float angle { get; private set; }
3939

40+
/// <summary>
41+
/// The update mode during which the object orbits.
42+
/// </summary>
43+
[Tooltip("The update mode during which the object orbits.")]
44+
public UpdateMode updateMode = UpdateMode.Update;
45+
4046
private void Start()
4147
{
4248
this.angle = this.startAngle;
4349
}
4450

51+
private void Update()
52+
{
53+
if (this.center != null && this.updateMode == UpdateMode.Update)
54+
{
55+
this.angle += this.speed * Time.deltaTime;
56+
SetPosition(this.angle);
57+
}
58+
}
59+
4560
private void LateUpdate()
4661
{
47-
if (this.center == null) {
48-
return;
62+
if (this.center != null && this.updateMode == UpdateMode.LateUpdate)
63+
{
64+
this.angle += this.speed * Time.deltaTime;
65+
SetPosition(this.angle);
4966
}
67+
}
68+
69+
private void FixedUpdate()
70+
{
71+
if (this.center != null && this.updateMode == UpdateMode.FixedUpdate)
72+
{
73+
this.angle += this.speed * Time.fixedDeltaTime;
74+
SetPosition(this.angle);
75+
}
76+
}
5077

51-
this.angle += this.speed * Time.deltaTime;
52-
float radians = this.angle * Mathf.Deg2Rad;
78+
private void SetPosition(float angle)
79+
{
80+
float radians = angle * Mathf.Deg2Rad;
5381

5482
this.transform.position = new Vector3(
5583
x: this.center.position.x + (Mathf.Cos(radians) * this.radius),
5684
y: this.center.position.y,
57-
z: this.center.position.z + (Mathf.Sin(radians) * this.radius)
58-
);
85+
z: this.center.position.z + (Mathf.Sin(radians) * this.radius));
5986
}
6087

6188
}

0 commit comments

Comments
 (0)