0
\$\begingroup\$

I have to show complete 24 hours cycle in Unity3d Scene. Where different kind of task will be executed in different Times.

Suppose

  • at 2pm I have to move a car,
  • at 6pm An Aeroplane will land on airport
  • at 07:07pm I have to open the door of Market's Buildings
  • and so on different task/works execution at different timing.

Now, I have some basic animation e.g., car, Aeroplane and door etc.,. Now I am confuse that how to play and stop my animation according to the time. And time can also be changed manually using GUI. If currently 6pm (animation working) then user can switch to 8am to view 8am time's animation (or code execution). And User can also fast the timing to view complete hours in a single minutes or in an hour.

I can use if/else in Update Events but I guess this is not the right way to do and will be very difficult if I require to show significant number of works in different time duration (which means significant number of if/else statements). Just like below

void Update(){
if(time =1){
//logic 
}
if(time =2){
//logic 
}
if(time =3){
//logic 
}
...//so on, tedious way 
...//and also not possible if time require between the hours, suppose 06:06pm
}

What to do ? How to handle this?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The way I'd approach this is to create a MonoBehaviour representing a daily event. We'd attach an instance to each object that needs to do something at a particular time.

This DailyEvent behaviour would expose to the Inspector:

  • a start time
  • an end time or duration (can be read implicitly from the animation, if using)
  • some type of payload, like an animation to trigger or an event/message to send

You might have a few subclasses to specialize eg. DailyAnimationEvent vs DailyToggleEvent, or just handle them all in one class - that's up to your coding style.

Then I'd have a master DailyClock script that handles time of day. On startup, this script would find all DailyEvent instances in the scene and add them to an efficient lookup structure like an Interval Tree.

When the time changes, the DailyClock script is responsible for finding all DailyEvent intervals in its lookup structure overlapping the current moment, and telling them to trigger or update their animation playhead position. It also needs to find all formerly active DailyEvent intervals and deactivate them, for when the time seeks outside of a DailyEvent's interval.

Now all of the logic about when to trigger certain events lives in your data, rather than a big nest of if statements in code. This makes it easier to scale to bigger & smaller scenes, since you can reuse the same code with different scene objects.

This also keeps the timing information attached to the object that needs it, so you don't need to jump around your scene when editing the plane landing - the plane object has it all in one place - and if you delete an object its timing logic goes with it, rather than hanging around as unused cruft.

\$\endgroup\$
0
\$\begingroup\$

You could use Time.timeScale and your own time provider/clock to achieve this. You will need to store the animations in a list to be able to know their start time and also create the animations with descent play speed in mind. Update your clock according to your current play speed in your game loop. If the user steps in time just loop through the list of animations and set the animations normalized time if your clock is changed to a time where the animation should play. Use TimeScale to change playback speed or create your own time provider and control the animation playback yourself.

// pseudo code
void SetTime(float clock) {
    for each anim in anims {
        If(clock >= anim.start && clock < anim.end) {
            anim.normalizedTime = clock/anim.end;
            anim.Play();
        } else {
            anim.Stop();
        }
    }
}

http://docs.unity3d.com/ScriptReference/Time-timeScale.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .