forked from Gkxd/Rhythmify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveToPositions.cs
More file actions
89 lines (80 loc) · 3.07 KB
/
Copy pathMoveToPositions.cs
File metadata and controls
89 lines (80 loc) · 3.07 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
/* @Author: Gkxd
*
* Moves a GameObject to a list of specified positions
*
* */
using UnityEngine;
using System.Collections;
namespace Rhythmify {
public class MoveToPositions : _AbstractRhythmObject {
public Vector3[] positions;
public int[] indices;
public int offset;
public bool local;
public bool relative;
public bool rigid;
private Vector3 startPosition;
private Rigidbody rigidBody;
override protected void init() {
if (relative) {
if (local) {
startPosition = gameObject.transform.localPosition;
}
else {
startPosition = gameObject.transform.position;
}
}
else {
startPosition = Vector3.zero;
}
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 = positions.Length;
if (size <= 1) {
return;
}
int idx = beat + offset;
if (indices.Length > 0) {
int idxA = indices[idx % indices.Length];
int idxB = indices[(idx + 1) % indices.Length];
StartCoroutine(move(positions [idxA % size], positions [idxB % size], secondsPerBeat));
}
else {
StartCoroutine(move(positions [idx % size], positions [(idx + 1) % size], secondsPerBeat));
}
}
private IEnumerator move(Vector3 startPos, Vector3 endPos, float duration) {
float startTime = Time.time;
if (rigid && rigidBody != null) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
rigidBody.MovePosition(Vector3.Lerp(startPos, endPos, lerpPercent) + startPosition);
yield return null;
}
}
else if (local) {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.localPosition = Vector3.Lerp(startPos, endPos, lerpPercent);
transform.localPosition += startPosition;
yield return null;
}
}
else {
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
transform.position = Vector3.Lerp(startPos, endPos, lerpPercent);
transform.position += startPosition;
yield return null;
}
}
}
}
}