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 SenjuDahr · Mar 27, 2014 at 07:44 PM · c#animationcallback

Unity 2D: perform action after animation is done.

Hello everyone,

I have a question regarding a throwing/shooting animation for a 2D platformer game. I have this "throw" sprite animation that I want to play when the user presses the fire button. The animation itself is such that there is an anticipation phase where the character leans back to put force behind his throw, followed by a forward motion where the character actually throws the object.

I was wondering whether it is possible to play the animation when the user presses the fire button and then only AFTER the animation has played out, create a "bullet" object that will fly through the scene. If it is possible, how would this be done in C# scripting? Is there some sort of callback mechanism I can use?

Many thanks in advance!

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

2 Replies

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

Answer by Denvery · Mar 27, 2014 at 09:49 PM

You can use Animation events: https://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html

So when your "throw" sprite animation finish, specified function is called. This function may create a bullet object etc. :)

As for me it is the most simple way.

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 SenjuDahr · Mar 28, 2014 at 05:30 PM 0
Share

Thanks, this was what I was looking for :-)

avatar image Denvery · Mar 29, 2014 at 02:00 PM 0
Share

You are welcome!:-)

avatar image
0

Answer by Destran · Mar 27, 2014 at 07:51 PM

You could use a Coroutine :D

  void Update()
     {
       if(conditions to throw are met)
         {
         StartCoroutine(Throw());  
         }
     }
      
     
     
     IEnumerator Throw()
     {
         animation.Play("throw");
         yield return new WaitForSeconds (animation["throw"].length);
     
     }
 
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 Destran · Mar 27, 2014 at 07:53 PM 0
Share

I think this is correct c#, but I don't actually know c# that well so... :D

avatar image SenjuDahr · Mar 27, 2014 at 09:27 PM 0
Share

I did not know about CoRoutines. I will look into it. Thanks very much!

avatar image farshidhss · May 04, 2018 at 06:34 AM 0
Share

This is incorrect! if your frame rate drops then the animation takes longer time to complete but WaitForSeconds does not depend on frame rate and it would return.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Animation callbacks and/or events? 2 Answers

Setting local position after animation is inconsistent 0 Answers

Multiple Cars not working 1 Answer

How to get the Quaternion Rotation from a Matrix4x4 2 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