- Home /
Count time between scene executions
The situation: I have multiple "sector" scenes with moving objects that should keep "moving" in and between sectors even when player is in another one and to do so I have planned to gather all the destination, speed and position datas and recalculate them by time.
What I need is to know how many time passes between a scene destroy and its next load
Answer by FM-Productions · May 04, 2017 at 02:01 PM
You could make a static class that stores the time of the scene destruction with a static function that will return the time that has passed between the scene destruction and the load. Then when you destroy the Scene, set the current time (you could use Unitys Time.time) as destruction time. When you load the new Scene, maybe in the Start() function, call the function getTimeBetweenDestructionAndCurrentTime
to get the time difference (pass the current time as parameter), then use this value to recalculate the positions of your objects.
public class SceneTimeTracker {
private static float sceneDestructionTime;
public static void setSceneDestructionTime(float time){
sceneDestructionTime = time;
}
public static float getTimeBetweenDestructionAndCurrentTime(float timeNow){
return timeNow - sceneDestructionTime;
}
}