How to wait until an animation is finished.

Hi! i know this question is asked every other week, but i searched the answers database and none of the answers i found were working for me and i didn’t want to comment on an old thread so I’ll just ask this again, how do i make this wait until the animation is finished?

function Spawn()
{
	animation.Play("DIE");
	rigidbody.AddForce(Vector3.down * 200);
	transform.position = spawn.position;
}

NOTE 1:
I’ve tried putting the second part of the code (
rigidbody.AddForce(Vector3.down * 200);
transform.position = spawn.position;
)

on a diferent function and then calling that from the unity animation panel but that didn’t seem to work either =/

NOTE 2:
I’m a noob in unity and javascript so please bare with me ^^

In a coroutine based on time:

  function Play()
  {
      animation.Play("clip");
      yield WaitForSeconds(animation["clip"].length * animation["clip"].speed);
      //Do something when the animation is complete
  }

I actually had to do this recently for my micro-script I put up on the Unity Asset Store: Animation Caller

However, what you can do for the script you have listed is to use the boolean function:

function Spawn()
{
    animation.Play("DIE");
    rigidbody.AddForce(Vector3.down * 200);
    transform.position = spawn.position;
}

function Update()
{
    if (!animation.IsPlaying("DIE"))
    {
         // The animation is finished
    }
}

Reference IsPlaying

you can also replace the if statement inside of the update function with just:

function Spawn()
{
    animation.Play("DIE");
    rigidbody.AddForce(Vector3.down * 200);
    transform.position = spawn.position;
}
    
function Update()
{
    if (!animation.isPlaying)
    {
         // No animations are playing
    }
}

Reference isPlaying

I’ve got a c# answer for this @captainvera It’s using the legacy animation system but this will work in Unity 5.6.

public string[] animClips = new string[2];
public float speed = 2.0f;
private Animation anim;

    void Start()
    {
        anim = GetComponent<Animation>();
    }

    private void Update()
    {
        if (Input.GetKeyUp("a"))
        {
            StartCoroutine(Animate());
        }

    }

//use a coroutine so we can pause this function and wait for the animation to complete
    public IEnumerator Animate()
    {
          anim[animClips[0]].speed = speed;
          anim.Play(animClips[0]);
          yield return WaitForAnim(anim[animClips[0]], speed);
    }

    IEnumerator WaitForAnim(AnimationState animclip, float spd)
    {
        tempTime = animclip.length * (1 / spd);
        yield return new WaitForSeconds(tempTime);
    }

Try this method

void FunctionThatCallsYourAnimation()
{
    Animator anim = YourGameObject.GetComponent<Animator>();
    anim.Play("YourAnimationClip");
    Invoke("ToDoAfterAnimationPlayed",lengthofanimation);
    //float lengthofanimation = length of animation * speed of animation 
}
void ToDoAfterAnimation()
{
     // Code to be executed after Animation is played
}