- Home /
animation delay or wait
i am new to scripting so can anyone give me an example or any help of how i can delay an animation.
Answer by MC HALO · Nov 29, 2010 at 04:10 AM
yield WaitForSeconds(4)
put this code before the animation and it should start after 4 seconds :) or you can make a simple script like so:
function Start() {
animation.Play("Put Your Animation name here ");
}
Now the above script will trigger the animation as soon as you play the game. now if you want to delay the animation you do this:
function Start() {
yield WaitForSeconds(2);
animation.Play("Put Your Animation name here ");
}
now this will do the same as the code above but this time it will delay the animation for 2 seconds
hope this helps:)
i see i get it but the function why did u put start, isnt it function update
Yea you can use it in update as well i just did this because it is like an example. to help you :)
Is there any way that I could get an example in C#? Im trying to do this exact thing and my poor attempt at converting javascript to C# doesn't work.
Hi, i believe the above method is obsolete and you should use the one I mentioned in the post below - including the set-up in animator. I posted the c++ code for that. If you want to try the above regardless you should be able to get the c++ needed items from my code below: mainly StartCoroutine(DelayedAnimation()); //and IEnumerator DelayedAnimation () { yield return new WaitForSeconds(startDelay);
hope that helps
Answer by Happy-Zomby · Nov 20, 2014 at 09:26 AM
0 if you are using the more recent Animator you will have to combine 2 states to use the above.
Locate the animator window you should see your animation there and be able to create a new empty State (right click on grid area and select "creat state" / "empty"), call in something like "waiting". then right-Click it and select it has default state (becomes orange) Then in your script:
var startDelay : int;
function Start()
{
var animator = GetComponent(Animator);
yield WaitForSeconds(startDelay);
animator.Play("animation_I_want_to_play");
}
animation_I_want_play should match the name of your animation/state in the animator window.
old post but I stumbled over it recently so it hope this helps,
Same code as above but in C++,
public int startDelay;
Animator animator;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
StartCoroutine(DelayedAnimation());
}
// The delay coroutine
IEnumerator DelayedAnimation ()
{
yield return new WaitForSeconds(startDelay);
animator.Play("animation_I_want_to_play");
}
Thanks!
with the animator.Play("animation_I_want_to_play") am I targeting the animation controller attached to the object? I can stop the animation but it wont play.
i can't recall the exact details of how I tested this at the time but yes I believe you should have the animator component on the object itself. If on a child you can use getcomponentinchildren (or something like that). When testing you need to look at the animator window - (Window - animator) and you should see if your "states" changes to orange and indicates a playing bar.
Answer by rrivas2009 · Jun 05, 2020 at 07:35 AM
Here's a way that worked for me, in C#,
public class StartAnimation : MonoBehaviour {
Animator m_Animator;
void Start()
{
m_Animator = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Time.timeSinceLevelLoad >= 10) {
m_Animator.Play ("MyAnimation");
} else {
m_Animator.Play ("Stop"); //Here another state
}
}
}
Answer by wohoyefe · Jul 17, 2021 at 06:01 AM
Hi everyone. Does anyone have an update on how this currently works step by step? I have a cube called "cube", an animation called "123" and an animation controller called "456". The animation itself is the cube moving for a couple of meters. How do I go from here? Step by step. I've been trying every single code in the previous answers here for the last 4 hours but my cube just keeps ignoring my scripts and moves right after starting the game. The cube has an animation controller and the script is assigned to the cube too. Any help is really appreciated, thank you so much