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
1
Question by Mifodii · Jan 30, 2017 at 03:36 PM · animationanimation events

Are the Animation Events the only way to go?

Hello everyone.

So, I'm working with Unity for some time now and most of the things are nice, but some of them seem quite strange for me. Maybe this will make you laugh and all, but I would love to hear your point of view.

My question is about unity Animation Events. From what I understand, they're the main, if not the only, way to execute some code at the specific moment of an animation. Maybe I'm wrong, but, honestly, that bothers me. I worked on multiple projects involving improving code of other people who utilized animation events, and in most cases the thought "Oh, that must be executed from the animation clip" was far from obvious. The necessity of searching the project for the right animation clip to edit some behaviour still looks like a joke to me when we're talking about easily readable code and such. This situation brings my memories of Flash days, when you could do the similar thing - place the code in the animation frames of clips. I can't even start to explain how frustrating this is.

So, good, honorable and experienced people of Unity. Can you please tell me where I'm wrong? Is there any other method of designing animation-related behaviours and I'm just overlooked some API? Please, point me to the right direction! (All I saw for now is AnimationState.normalizedTime and it's harldy an alternative.) Or are you not bothered at all with animation events? Maybe there are some methods to work with events and avoid frustration. Share them, please! I would love to hear any thoughts on this subjects, thank all of you 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jan 30, 2017 at 04:25 PM

I don't have an issue with them. Although I normally prefer to see things in script rather than in a GUI, I'm not sure how else you're proposing, say, firing an event on exactly the frame in which a character's foot touches the floor other than by attaching it to the animation clip itself? Using code to attach an event to an arbitrary frame number or timestep through the animation seems much more obtuse to me.

To be clear, the only code I place in the animation clip is an event handler. So, in the frame that the foot touches the floor, I fire the OnStep() event. The code that plays the footstep SFX, leaves a footprint, or produces a dustcloud are nowhere in the animation clip - they live in their own classes and simply subscribe to the OnStep event. If you've got any logic or substantial code in your animation clip handler then I'd say you're doing it wrong.

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 Mifodii · Jan 30, 2017 at 04:45 PM 0
Share

Thank you for your answer!

Do you have any conventions about using such events? I mean, if I open your code, I see only event handlers, which are listening to, for instance, OnStep and doing something with it. How am I supposed to know who are firing it? If I want to change the behavior in some way, how I supposed to know where to look? You can have dozens of animation clips for your character, and opening each of them searching the event and altering doesn't seem like a good practice.

avatar image
0

Answer by Meatloaf4 · Sep 17, 2017 at 05:20 PM

@Mifodii You are indeed correct, it can be very hard to track down these functions do to the constraints you stated. As is often the case with Unity, the power they give you to visually produce things rather then doing them in code comes with a price.

This is where conventions come in, I do think that Unity should have a much better bug tracking system in place for this as well 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
avatar image
0

Answer by spesifikbrush · Nov 20, 2020 at 11:01 AM

I know this is an old question, and I don't know if this situation got better, but this is what I do when I'm dealing with 2D sprite sheet animations:

  • Reference the AnimationClip object.

  • Get the time between frames with 1f / clip.frameRate

  • Specify the frame where I want to fire an event (for example, weapon animation wind up ends at frame 5, frame 6 will be where the attack collision check should happen)

  • With a simple timer logic called in update (more precisely, in a state machine), execute code when timer hits the point.

It works really well. It may work with non-sheet animations, but I don't know how to integrate transition, exit times, or if it would break with blend animations or not.

As @tanoshimi suggested, simply creating C# events and subscribing to them would be much simpler. Maybe if Unity turns AnimationEvents to ScriptableObject assets, it would be much simpler.

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

8 People are following this question.

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

Related Questions

Animation events firing all at once in mirrored blend tree 1 Answer

How can you check for player input during specific frames of an animation? 1 Answer

multiple Animation Events in an Animation 0 Answers

Animation Events added with code seem to be at wrong time 0 Answers

Is there a way to select animation events on the dopesheet beside clicking on them? 0 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