There are many such objects which fall under the tag ‘blue’, and each and every entity has a different length of journey to reach a goal object. Upon reaching the goal object, the input control for each object gets disabled, so as to allow the rewind and replay to happen. How do I create a global time variable, which I can connect to all these objects, that I can just decrement to zero, so as to rewind these objects, and to replay them again, when the rewind is done happening, and all the blue tagged objects have reached their first waypoints simultaneously ??
This is the type of question I hang around waiting for.
A couple of design questions to consider
Do you need interpolation?
Will your time step always be fixed at 0.2?
Will the replaying be occurring while other GameObjects are still moving?
Do you need multiple instances running or can you make things static?
Here is the basic structure I would use
Create a manager class and a waypoint class
Collect all of the waypoints to be managed in a List
Each waypoint should check in from OnEnable, and out in OnDisable
Each waypoint will have a function that can set its position according to a time
The manager class will call the new time on every waypoint in the list
Here is some pseudo code to get you started
// This class is static. Call TimeManager.SetTime from anywhere.
public static class TimeManager {
private static List<Player> players = new List<Player>();
public static void AddToList (Player player){
players.Add(player);
}
public static void RemoveFromList (Player player){
players.Remove(player);
}
public static void SetTime (float time){
foreach (Player player in players){
player.SetTime(time);
}
}
}
// This class gets added to each GameObject to be managed
public class Player : MonoBehavior {
public List<Vector3> waypoints = new List<Vector3>();
void Start() {
InvokeRepeating("addWaypoints", 0, 0.2f);
}
void addWaypoints(){
if (!Input.GetKey("space")) waypoints.Add(transform.position);
}
void OnTriggerEnter (Collider obj) {
waypoints.Add(transform.position);
CancelInvoke("addWaypoints");
gameObject.tag = "blue";
TimeManager.AddToList(this);
}
void OnDisable (){
TimeManager.RemoveFromList(this);
}
public void SetTime (float time){
int index = (int)(time / 0.2f);
if (index >= waypoints.count){
Debug.LogError ("Time outside of allowed range, clamping")
index = waypoints.count;
}
transform.position = waypoints[index];
}
}
You will have to adjust if you decide you want flexible time steps. That will only be doable using a time stamp.
I haven’t coded in interpolation. To do this you take the index and the index + 1, then lerp based on the difference of time from the lower time step. Let me know if you want pseudo code for this.
I made the manager class static as it seems to be you would only have one of these running around. But you don’t have to do it this way.
If you want to be still moving GameObjects while setting the time for others you will need to move adding them to the list out of OnEnable as appropriate.