How do I delay the animation?

Hi everyone,

After How do I rotate the object? I decided to chose the animation option. But I can’ find out how to make it wait before starting like it was in obstacle course fallers.
My current code is

public class Shuttle : MonoBehaviour

{

    [SerializeField] float timeDelay = 10f;

   

    private Animation anim;

   

    void Start()

    {

    anim = GetComponent<Animation>();

    anim.enabled = false;

    }

    // Update is called once per frame

    void Update()

    {

        if(Time.timeSinceLevelLoad > timeDelay)

       {

        anim.Play("PatrolHeli");

       }

    }

   

}

UPD: For now, I solved this just via the timeline but it seems to be not the best solution possible.

  • There is no easy tuning for the starting moment
  • This pause will be playead after each series of loops. It’s more than enough to complete the level, but still…

Hi Peter,

It’s great to see that you are challenging yourself. :slight_smile:

Does your timeline work? If so, that’s a fine solution. The only potential problem I see is the if-condition in the code which will have Unity execute the Play method each frame if the condition is true. I do not know if this restarts the animation.

If you are not happy with your current solution, you could use the Animator (instead of the Timeline) with the “Idle” and a new state, e.g. “Patrol”. You define a bool parameter and use it as the condition for the Idle → Patrol transition. And you enable “loop” in the animation. This way, you could simply set the bool to true in your code to have the animator change the state from “Idle” to “Patrol”.

If (Time.timeSinceLevelLoad > timeDelay) is your condition, maybe you could start a coroutine or the Invoke method in the Start method() instead of checking this condition each frame in the Update method. After x seconds, you could set the aforementioned bool to true to have the Animator run the “Patrol” state/animation.

If you have a more complex idea, you could define your rules in a coroutine, e.g. play the animation, pause, play the animation, pause. A coroutine allows you to implement delays.

Maybe you don’t even need any code. Check the transitions in the animator. Maybe it is possible to define that the idle state should be left after x seconds. In the transition settings, it is also possible to define how many times an animation is supposed to be played before the state gets exited.

The timeline is great if you want to synchronise multiple animation tracks, e.g. a player flying around in the world, then making enemies appear after x seconds and do things, etc.

The animator is great if you want to make things happen based on conditions, e.g. “if x happens, play this animation; if y happens, play that animation”.

1 Like

Hmm will have to try it later, but for now this timelime works pretty fine.
For the current project I solved it gaeplay-wise by adding another enemy to the timeline so the player won’t be able to idle long enough to see the animation stop. I needed this part anyway, so the timeline looks more suitable in my case.
Thank you!
(something tells me I’ll be back soon with other questions)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms