forked from Gkxd/Rhythmify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateToEulers.cs
More file actions
107 lines (96 loc) · 4.05 KB
/
Copy pathRotateToEulers.cs
File metadata and controls
107 lines (96 loc) · 4.05 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
/* @Author: Gkxd
*
* Rotates a GameObject to a list of specified Euler angles
*
* */
using UnityEngine;
using System.Collections;
namespace Rhythmify {
public class RotateToEulers : _AbstractRhythmObject {
public Vector3[] eulerAngles;
public int[] indices;
public int offset;
public bool local;
public bool rigid;
public bool spherical;
private Rigidbody rigidBody;
override protected void init() {
if (rigid) {
rigidBody = gameObject.GetComponent<Rigidbody>();
if (rigidBody == null) {
Debug.LogError("The GameObject " + gameObject + " has no RigidBody component attached!");
Debug.Break();
}
}
}
override protected void rhythmUpdate(int beat) {
int size = eulerAngles.Length;
if (size <= 1) {
return;
}
int idx = beat + offset;
Quaternion startRot;
Quaternion endRot;
if (indices.Length > 0) {
int idxA = indices[idx % indices.Length];
int idxB = indices[(idx + 1) % indices.Length];
startRot = Quaternion.Euler(eulerAngles [idxA % size]);
endRot = Quaternion.Euler(eulerAngles [idxB % size]);
}
else {
startRot = Quaternion.Euler(eulerAngles [idx % size]);
endRot = Quaternion.Euler(eulerAngles [(idx + 1) % size]);
}
StartCoroutine(rotate(startRot, endRot, secondsPerBeat));
}
private IEnumerator rotate(Quaternion startRot, Quaternion endRot, float duration) {
float startTime = Time.time;
if (spherical) {
if (rigid && rigidBody != null) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
rigidBody.MoveRotation(Quaternion.Slerp(startRot, endRot, lerpPercent));
yield return null;
}
}
else if (local) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.localRotation = Quaternion.Slerp(startRot, endRot, lerpPercent);
yield return null;
}
}
else{
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.rotation = Quaternion.Slerp(startRot, endRot, lerpPercent);
yield return null;
}
}
}
else {
if (rigid && rigidBody != null) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
rigidBody.MoveRotation(Quaternion.Lerp(startRot, endRot, lerpPercent));
yield return null;
}
}
if (local) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.localRotation = Quaternion.Lerp(startRot, endRot, lerpPercent);
yield return null;
}
}
else {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.rotation = Quaternion.Lerp(startRot, endRot, lerpPercent);
yield return null;
}
}
}
}
}
}