Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by captainvera · Mar 28, 2013 at 01:08 AM · animationjavascript

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 ^^

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rodimus_strom · Mar 28, 2013 at 01:49 AM 0
Share

try animation.IsPlaying("animation name") it return true/false. like: if(!animation.IsPlaying("Idel")) { //animation stop } else { //still playing }

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by whydoidoit · Mar 29, 2013 at 12:28 AM

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
   }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 29, 2013 at 12:28 AM 0
Share

It's in the docs :) http://docs.unity3d.com/Documentation/ScriptReference/AnimationClip-length.html

avatar image mb28 · Nov 10, 2017 at 07:20 PM 0
Share

To get an accurate accounting of the animation speed, you need to multiply it by 1/speed. In Unity a speed of 2.0 is twice as fast while a speed of 0.5 is half speed.

$$anonymous$$ultiplying the length of an animation clip by speed won't produce the correct results. 1 second animation clip at speed of 2.0 should only be 0.5 seconds long.

 1 * (1 / speed)


Hope this helps clarify for people searching for this

avatar image tBurger · Jul 01, 2020 at 02:47 PM 1
Share

also don't forget Time.timeScale!

avatar image
0

Answer by BeardedManBrent · Mar 28, 2013 at 02:40 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image captainvera · Mar 28, 2013 at 11:03 PM 1
Share

but if i put the rest of the respawn code in that if statement, it will executed it whenever the animation is not playing and the player will be always respawning!

avatar image
0

Answer by mb28 · Nov 10, 2017 at 07:18 PM

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);
     }
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Faihan · Jul 01, 2020 at 02:29 PM

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
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] Shooting fail (Due to camera setup) 2 Answers

How can I tell Unity to wait to do something until after and animation has finished playing? 1 Answer

[SOLVED] Shooting from Elemental Staff 1 Answer

How to get a animation to play without changing the pozition the gun is in when aming down sights 0 Answers

[Solved] Generating Particles with Attack Animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges