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
7
Question by Jinxology · Oct 10, 2014 at 11:07 PM · animationspriteseventsfiring

Animation events not firing

I'm having a problem using animation events for a looping 2D sprite animation. While the events usually fire, they don't always fire.

For example, I have a sprite that fires "StartCombatAnimation()" on frame 1, and "EndCombatAnimation()" on the 2nd last frame. That end event fires only about 98% of the time. It fired even less when I had it on the last frame, which is why I moved it up a bit.

As I increase Time.timeScale (ie. say 5x normal time), it misses that event even more, leading me to believe that the frames are being skipped, and therefore the events are being skipped.

I'm looking for advice for a way to reliably force events to fire at the beginning, during, and/or at the end of an animation?

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 Eluem · Nov 05, 2015 at 08:11 PM 0
Share

Since it seems like animation events are skipped when frames are skipped and everyone dislikes this, please take the time to vote on this suggestion to add a feature which will allow us to choose to make our animation events guaranteed:

https://feedback.unity3d.com/suggestions/animation-event-guarantee

3 Replies

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

Answer by b1gry4n · Oct 11, 2014 at 01:57 AM

I dont like relying too much on animation driven events just because they can sometimes hiccup like this. I've found its much easier to just make your own "control" over the events by using a coroutine.

I am guessing you are triggering the "StartCombatAnimation()" as an attack of some sort.

     private bool attacking = false;
     public float animLength = 1.5f; // adjust this to fit your attack animations length
 
     void Update ()
     {
         if(Input.GetButtonDown("Fire1") && !attacking )
         {
             Attack();
         }
     }
     
     void Attack(){
         attacking = true;
         //Trigger the animation here
         //Trigger the start animation events here
         StartCoroutine(Attacking());
     }
     
     IEnumerator Attacking(){
         yield return new WaitForSeconds(animLength );
         // trigger the stop animation events here
         attacking = false;
     }

To better understand why this is possibly happening it would require us to see the code that triggers your attacks. If you are crossfading or blending animations into one another its most likely the frame that triggers your event is getting cut out.

Comment
Add comment · Show 4 · 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 Jinxology · Oct 11, 2014 at 07:37 AM 0
Share

Thanks, I'll give the coroutine a shot. Regarding my code, I'm not crossfading or anything, it's just looping the animation and not firing the events consistently.

avatar image Eluem · Nov 05, 2015 at 08:11 PM 0
Share

Since it seems like animation events are skipped when frames are skipped and everyone dislikes this, please take the time to vote on this suggestion to add a feature which will allow us to choose to make our animation events guaranteed:

https://feedback.unity3d.com/suggestions/animation-event-guarantee

avatar image BadSeedProductions · May 26, 2017 at 04:37 PM 0
Share

What I ended up doing, and it seems to be working (*fingers crossed) is adding multiple of the same events on the same animation, so that if it missed the first, surely it would hit the second or 3rd. This will only work in some cases, like my case which was using the event to make a bool value false, and it didn't particularly matter WHEN, just as long as it DID. So far the event seems to be firing consistently now.

avatar image tubelightboy · Oct 29, 2020 at 06:27 PM 0
Share

I think it is wise to stop the coroutine after starting it. I think it can be done right after the trigger event.

avatar image
24

Answer by sampenguin · Sep 04, 2015 at 06:14 AM

Related: I had this problem on events not firing on last frame of a single shot (i.e. not looping) animation. I could fix it by moving the event a few frames forward, but the engineer in me did not like the arbitrary nature and guessed it might be intermittent behavior due to framerate. So I kept digging until I found reliable behavior.

In my case, in the state machine, I had my animation state transitioning to Exit. Apparently this causes some kind of blending to happen (even though I have no other animations or layers in this case, and no explicit blending), which can skip over n number of frames at the end. I deleted the transition to Exit in Animator, and now my Event fires 100% of the time on the final frame.

So I'm guessing any kind of blending activation that occurs has the possibility to drop frames, and if there's an AnimationEvent on a frame that's dropped... guess what, that AnimationEvent doesn't get triggered!

Hope that helps someone else!

This should be categorized as a bug, but judging from how old this issue is from the posts I have seen about it, I doubt it will be fixed as such. If it's by design, it's a poor choice to have the possibility of AnimationEvents getting missed due to blending... these should be guaranteed to hit.

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 joseph_apitome · Apr 18, 2017 at 01:08 AM 0
Share

This answer is correct. The way I fixed it was in the Animator of the transition back into the default state (where Has Exit Time is checked), I set the Exit Time slightly longer than the actual animation itself. ie my animation was 1 second so the Exit Time was set to 1.1 seconds

avatar image
6

Answer by Deepscorn · Sep 14, 2015 at 10:33 AM

 public class CombatBehaviour : StateMachineBehaviour {
 
      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
       StartCombatAnimation();
     }
 
     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
         EndCombatAnimation();
     }
 }
 

See http://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html for more. That behavior must be added as a component to animator state with your animation. That works 100 % of the time

Comment
Add comment · Show 6 · 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 JoeStrout · Sep 23, 2015 at 08:31 PM 0
Share

Yeah, but the difficulty with this is that state behaviours can't reference any properties in the scene — even on the object the state machine is attached to. This is because the state machine actually lives in the project, not in the scene at all. This can make it a PITA to use compared to animation events.

(But, I guess it's the best work-around given the unreliability of animation events!)

avatar image Chris-Clark JoeStrout · Nov 02, 2015 at 05:04 AM 1
Share

You can reference the object it's running on actually. 'animator' references the Animator on the GameObject that's running the current animation.

An example from one of my State$$anonymous$$achineBehaviour classes:

 override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
 {
     humanoidController = animator.GetComponent<HumanoidController>();
 }

From there on I can call a public function humanoidController.SwingingSword() or something similar.

avatar image JoeStrout Chris-Clark · Nov 02, 2015 at 02:07 PM 2
Share

Yes, you can certainly do this in code. I only meant that you can't reference scene objects/properties in the inspector.

Show more comments

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

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

Playing a different sprite prefab on collission. 1 Answer

having an event play at a certain time durring an animation? 0 Answers

How to swap sprites? 1 Answer

Changing how my character looks depending on his current lifes. 2 Answers

2d sprite animation issue 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