Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NDJ23 · Jun 25, 2011 at 06:33 AM · animationcoroutinesfinish

Finish Animation before continuing script

I have an enemy with a collider trigger. When the enemy gets hit by a bullet, it reduces his hitpoints, pretty basic.

The issue I've run into is that when the Enemy's HPs are less than or equal to 0, I want to move the gameobject off the screen and disable it. All of this works great. I just created a "dead" animation when the enemy is killed, so he falls to the ground. The problem I have is that the animation plays after the object is moved and disabled, instead of before. I've tried different methods to fix this, but haven't been successful. I tried using coroutines(not sure how these work exactly), but couldn't get it working that way either. Here is the basic script I am working with:

  //Attached to an enemy
 function OnTriggerEnter(otherObject : Collider)
 {
      if(otherObject.gameObject.tag == "bullet")
      {
          if(EnemyHPs > 0)
          {
             animate.Play("Enemy_Hit");
             EnemyHPs--;
          }
          else if(EnemyHPS <= 0)
          {
            animate.CrossFade("Dead");
            if(!animate.IsPlaying("Dead"))
            transform.position = startingPosition;
            gameObject.active = false;
            EnemyHPs = 3;
          }
    }
 }

I also tried moving the code from the if statement "Enemy < 0" to the update function. I am probably way off track here, since I'm still learning how to code this type of stuff, but any help on how to get the animation to finish before the object gets disabled and moved would be greatly appreciated.

Thanks!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Ashkan_gc · Jun 25, 2011 at 07:28 AM

there are two ways to do that. the first way is to create an animation using unity's animation window located in window/animation. in this way you can attach a script to your animatino and execute it when you want (at the end). to read more on animation window click here. also you can add events to animations using code.

the second way is to run an animatino in a coroutine and wait until it's time and then execute your other code.

 function Die ()
 {
 animation.Play("die");
 yield WaitForSeconds (animation["die"].length);
 Destroy(this.gameObject); //destroys the object after animation ended
 }

this code assumed the name of animation to be dead. you simply can define a variable as var clip : AnimationClip and use it to drag the animation to your gameObject. also you can add 0.5 to the animation.length property to destroy the gameObject a litle after completing the animation. to add events by hand to specific times to animations also you need to use the AnimationClip class.

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 NDJ23 · Jun 25, 2011 at 06:55 PM 0
Share

Exactly what I needed, after playing with coroutines a little more, it's working great.

Thanks!

avatar image
1

Answer by testure · Jun 25, 2011 at 06:47 AM

This question gets asked at least once per day.

You use coroutines. I don't use javascript so I can't give you the exact code you can copy/paste, but here's the C# version of what you're trying to do (in a nutshell):

 void Update()
 {
   if(animationTime)
     StartCoroutine(DoAnimation());  
 }
 
 IEnumerator DoAnimation()
 {
   animation.CrossFade("MyAnimation");
   yield return new WaitForSeconds(2f); // wait for two seconds.
   Debug.Log("This happens 2 seconds later. Tada.");
 }

coroutines are easy, just need to spend a little time with them.

Comment
Add comment · Show 2 · 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 NDJ23 · Jun 25, 2011 at 07:10 AM 0
Share

Thanks for the help, it's greatly appreciated. I apologize about asking a question that is pretty common. I assure you I read as many posts as I could find, and tried using coroutines by trial and error, after a few hours though I still cound't get it to work.

I'll give it another go now that I know that's what I need to be using, and with your posts as reference as well.

Thanks again!

avatar image johnnyt · Sep 11, 2013 at 10:07 AM 0
Share

Ýou shouldn't start coroutines from Update which yield WaitForSeconds as Update is called every frame regardless of your coroutines. Ins$$anonymous$$d you should call a function from Update and start a coroutine from that function.

avatar image
0

Answer by vickygroups · May 06, 2014 at 09:27 PM

in the line, yield return new waitforseconds -- use the argument animationclip.length -- i couldn't get it to add 0.5 though.

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

6 People are following this question.

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

Related Questions

Can I make animations snap to a frame? 1 Answer

Wait for animation to finish 3 Answers

Stop an animation after its finished? 0 Answers

Make position of Buttons Elastic. 0 Answers

How to select an animation clip by index number? 7 Answers


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