1
\$\begingroup\$

I am using this script below to move an animation in unity with no root-motion. It simply walks across the terrain.

I have timer attached also so the animation doesn't start to walk for 60 seconds. This works fine in the scene when played…but when I navigate into this scene from another home scene the timer delay doesn't work as I guess it has been triggered already and has already reached the 60 second mark? I have tried using DontDestroyOnLoad but this just recreates the animation in the home scene also.

I'm trying to keep the code as simple as possible and maybe add to the code below to fix it:

void Update ()
{
    if (Time.time > 60)
    {
        transform.Translate (0, 0, Time.deltaTime);
    }
}
\$\endgroup\$
3
\$\begingroup\$

Time.time returns the number of seconds since the start of the game. "Start of the game" refers to the moment the game was launched, not the moment the current scene was loaded.

If you want this movement to start 60 seconds after a scene has loaded, use Time.timeSinceLevelLoad instead. This returns the time in seconds since the last level has been loaded. In other words, it starts counting up from 0 every time a scene is loaded.

void Update ()
{
    if (Time.timeSinceLevelLoad > 60)
    {
        transform.Translate (0, 0, Time.deltaTime);
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Another alternative is to kick this off in a coroutine in Start(), with an initial yield return new WaitForSeconds(60f); before the movement loop kicks in. \$\endgroup\$ – DMGregory Mar 28 '19 at 23:40
  • \$\begingroup\$ @DMGregory That is definitely a better solution. I had tunnel vision on the Time.time issue. \$\endgroup\$ – Alex Myers Mar 29 '19 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.