I need an object to move up and down according to a dynamic timing. The exact locations are stored in a List() called Timings. This will contain sorted entries such as 0.90 1.895 2.64 3.98... These timings are relative the playing of music, so they can be compared to TheMusic.time. (TheMusic is my AudioSource).
Right now (see code below), it's moving statically up and down using moveSpeed. How can I make it so that alternatively, the top and bottom point are reached at predefined times? When it reaches the end of the list of timings the movement should stop.
public class Patrol : MonoBehaviour {
public Transform[] patrolPoints; //contains top and bottom position
public float moveSpeed; //needs to be changed dynamically
private int currentPoint;
// Initialization
void Start () {
transform.position = patrolPoints [0].position;
currentPoint = 0;
}
// Update is called once per frame
void Update () {
print (currentPoint);
if (currentPoint >= patrolPoints.Length) {
currentPoint = 0;
}
if (transform.position == patrolPoints [currentPoint].position) {
currentPoint++;
}
transform.position = Vector3.MoveTowards (transform.position, patrolPoints[currentPoint].position, moveSpeed * Time.deltaTime);
}
}
It is important that there is no moving away from the absolute time point. E.g. when Timings reaches a high time such as 1009 there shouldn't be much drift.
Also note that other things (such as changing color and checking user behaviour) need to happen at the same time, see my other question.