3
3
namespace Zigurous . Animation
4
4
{
5
5
/// <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.
7
7
/// </summary>
8
8
public sealed class Orbit : MonoBehaviour
9
9
{
10
10
/// <summary>
11
- /// The transform the object rotates around.
11
+ /// The transform the object orbits around.
12
12
/// </summary>
13
- [ Tooltip ( "The transform the object rotates around." ) ]
13
+ [ Tooltip ( "The transform the object orbits around." ) ]
14
14
public Transform center ;
15
15
16
16
/// <summary>
@@ -37,25 +37,52 @@ public sealed class Orbit : MonoBehaviour
37
37
/// </summary>
38
38
public float angle { get ; private set ; }
39
39
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
+
40
46
private void Start ( )
41
47
{
42
48
this . angle = this . startAngle ;
43
49
}
44
50
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
+
45
60
private void LateUpdate ( )
46
61
{
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 ) ;
49
66
}
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
+ }
50
77
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 ;
53
81
54
82
this . transform . position = new Vector3 (
55
83
x : this . center . position . x + ( Mathf . Cos ( radians ) * this . radius ) ,
56
84
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 ) ) ;
59
86
}
60
87
61
88
}
0 commit comments