Happy Sunday! 🎉 Let's make an improvement on Unity's Animation Event system today! Check the repository for AnimationEvents from BlendTrees support as well.
Hey, I'm happy to see someone finally using StateMachineBehaviour! Would love to see you do a followup for everyone about extending ObjectPreview so those state machine behaviors can draw a preview of the animated character just like the Animator transitions do, without requiring modifying the scene character, and controlling that one's timeline.
@@git-amend Is this actually possible? I remember for one of the tools I made that I was not able to get a preview of an animation clip, ended up giving up in the end, it would have been a nice extra feature but if it actually a thing, I would love to go back and implement it!
@@X1N30F The preview window itself is doable with some effort. But, if you check the repository you'll find additional code for showing a blend tree animation in scene view that uses the Playable Graph (more functionality added after recording). Once you start making your own Playable Graph, you can do just about anything preview wise. Actually opening the preview window and using it requires using some reflection, but that window uses a similar technique.
@@git-amend thanks so much for getting back to me, I will definitely look into that and see how it goes! I might be able to just see what I could do to shoe in something for the preview window, since that's more so what I need rather than being in the scene view in any case I'm sure it will point me in the right direction.
I found it inconvenient to add animation events in AnimationEditor, so I created an animation wrapping class using ScriptableObject. I now see there's a way to add it directly to the state. Thank you for the helpful video.
Honestly, having watched a ton of youtube tutorials over my gamdev career, I can't think of better educator than you! Well explained, to the point, easy to follow. A+ 👌
I just discovered this channel, and the comments I see are very positive. I see why, bro. This right here is the truth. I am now a faithful subscriber!
This is great! I always hated how Unity handled these animation events, not only setting it up was painful but also you had no way of knowing before hand if that clip had an event inside it. You could easily forget when changing animation clips for better ones actually to setup the events again. Storing it within the Mechanic State Machine is definitely a better approach.
Just starting to work on my first game in unity. Definitely a challenge with a full time job but your tutorials are fantastic! Was just looking into animations as well 😊
Definitely the best channel for those who want to learn intermediate and high level developing skills! One thing I don't like about unity's state machine behavior is that, if there are two states blending between each other, the second state's OnStateEnter will actually be called before the first state's OnStateExit, this can cause bugs when script logic is highly depending on the animation's begin and end. (luckily, thx for git-amend's better state machine video, I extend that one to split the game logic and animation, really save my life)
Thanks so much for the comment! You're absolutely right-Unity's state machine can sometimes cause issues with overlapping `OnStateEnter` and `OnStateExit`, which is why splitting game logic from animation, as you've done, is such a great solution for avoiding those bugs. Thanks for sharing that!
This is fantastic and very timely in my case. This week, I've literally been going though and doing all those annoying tasks to make animation events manually.
Thank you so much for this, cumbersome is too polite a word I would use for Unity's animation system. I used AnimatorStateInfo to keep track of when animations were at varying points for things such as attacks or when I wanted logic to transition animations because Unity's Animation Event system either was too buggy or didn't respond at all when you wanted split second triggers. Love the addition of the custom editor!
Cool and useful video - as always) I tried this approach and discovered two interesting things: 1) The script can be slightly modified so that it takes all the sub state machines that are found in the main state machine - I played around a bit and it seems that something has worked out for me, which means that it will work for others as well) 2) I wonder what you are thinking of doing with the mixing trees, since several animations are used there and you can't add behavior to them, unfortunately... But I'll say it again - very, very cool video - thank you!
Thanks for the comment! I'm going to continue developing the script to handle some other things - handling substates is a good idea. For the blend trees, instead of normalizing a time, we can use the normalized weight param, and that part isn't too tricky - but displaying a preview is a little more difficult since there is no exposed method we can use for that. It will likely have to depend on some reflection. If I come up with a good solution for that, I'll be updating the repository.
This is really helpful, i tried a lot of stuff to avoid using the default animation events but i never thought of this, StateMachineBehaviors are so overlooked
Nice! Reminds me a bit of timeline signals too. I remember doing something similar like this a few years ago. Basically invoke unity events whenenver a state starts / stops etc. Seeing you write the editor tool code for this is very useful btw. I write a lot of editor code but just as often I think urgh editor stuff is so convoluted and badly documented, thanks for the learning opportunity.
@git-amend I've been using animation events on the fbx Importer. Setting up the timing has been a pain in the ass. This is going to be a pain and a lot of time to do it but it's definitely worth it
I am grateful for all the information you've given me but I can't help but be discouraged at the same time. You are insane. Every video I watch there is an ocean of information to take in. I was already satisfied at 4:50, but clearly you were not, and you went on to adjust InspectorGUI and HumanBodyBones. I didn't even KNOW that HumanBodyBones even existed. I didn't even know that I wanted all this before you showed it, and now it feels like a requirement. I thought knowing couple of common design patterns and a decent understanding of SOLID was enough for me to code efficiently, but there is so much knowledge out there that I don't know. If I'm trying to break into the video games industry, is this the level of Unity/C# (in your other videos) understanding I need to be competitive? Have I been doing nothing the past year? panik
Just keep in mind that the career of the game dev / software engineer is one of constant learning. You (and I) will never know everything, so just keep on learning new things every day! There is always more...
@@git-amend Will do... Just in case I didn't make it known enough, thank you so much for these high quality videos. I didn't know functional programming existed in C# until you showed it in your channel. I didn't know that you could edit the inspector until you showed it in your channel. I didn't know about the 'is a' / 'has a' relationship for composition vs inheritance until you showed it in your channel. Even though I am not at the level of mastery to fully understand/utilize all these concepts, this is the most helpful Unity channel for me BECAUSE it shows me what I don't know. I wish the best for you.
This is incredible! I was wondering if you had any ideas on how to handle Animation Override Controllers, and overriding the animation events to work with this set up?
That is a problem I haven't tackled yet. If I do, I'll make a video about it (or update the repository, make sure to enable notifications on the repo updates).
StateMachineBehaviors are pretty convenient when you remember they exist. I used them to make Animator play Timeline clips before I switch to Animancer plugin. I wish Unity just improved Timeline editor so it works more in line with Animator - support editing multiple timeline clips, not requiring Playable Director, being more convenient in general. Timeline allows to do much more things in more convenient ways, including stuff like in this video. Can wrap your read-only animation clips in timeline clips and it will work just fine. I hope new Dots animation system will solve this issue.
Nice, just discovered the channel and really enjoyed watching the videos. One thing I noticed in the latest 2 tutorials is that the top play bar inspector is changed. I tried to do that once but everywhere it was saying that specific editor part is locked to modification.
Awesome, glad to hear that. Check out the Time Scale Toolbar Free asset in the video description. If you download that, first of all it's great to use, but it also has an Editor script you can analyze called 'Toolbar Extender' to start making your own customizations.
You could've sold this as an asset for like 39.99 but not only you are giving it for free & and explaining every step... You are incredible man, to this community. I admire you. What would we do for eventName variable without strings? Since strings are easily mistyped, maybe enums? Or AnimationEventData SO reference instead of strings so that we can use it its name? Would it be overengineering.. IDK. Btw i couldnt see what we exactly changed on Animation Event property drawer. Seemed same to me as visually on the end.
@@damonfedorick I think I will keep this stand alone, at least for now. There are several improvements that can still be made: handling Animator Overrides and Layers, as well as support for passing parameter values as well. And possibly handling blend trees.
This is great, though there's a caveat that in some scenarios, events being tied to a specific animation file is actually _very_ useful. You can use one animator with several different NPCs with different animations, or like in my case, I've made a generalized sub-animator layer for handling weapons and items use, and what and when they activate their effects or sounds is determined by the animations they use... With script method you'd need to bake every single option into the animator, and clone animator for every new NPC, which immediately will make making edits to the animators a pain. My only gripe with the stock Animation Events is that they're gone when you re-import the animation. I wonder if it's possible to move this custom list of animation events into a separate external scriptable object and make it a dictionary of the animation files?
That's an interesting scenario. And also good to point out that they're gone after re-import. An approach of having a registry or dictionary might be one way to solve that, I'll have to give it a try as more features are added.
Nice video! May I request one on authoring cutscenes (Playable Director + timeline?). It has been a pain int the ass when it uses navmeshed and animator controller
Thanks for the suggestion! I’ll keep that topic in mind-working with NavMesh and Animator Controllers in Timeline certainly has its challenges, and a walkthrough could be helpful.
@@git-amend Nice! That would be awesome! There is an official repository where there are examples for this. Sadly, the timeline API it uses is badly documented (or not enough "straight to the point" for my ADHD :'D)
As others have pointed out, the Unity Animation Event system is hard to work with and is immensely cumbersome when animation clips need to be updated. Your solution seems fantastic and Unity should pay attention. I'm curious about the efficiency of this approach as the number of event state behaviors on a clip increases and the number of animated objects that handle events like this increases. Do you have any insight on if your solution is more or less efficient at runtime?
Thanks for the comment. This approach should be highly efficient at runtime for most use cases. As the scale increases, especially with hundreds or thousands of objects, it may require further optimization, but it’s unlikely to introduce significant performance issues in smaller-scale projects. I'll continue to optimize it as I think of improvements, or others suggest them. The only real cost is getting a reference to the Receiver component, which is minimal but could be optimized. In fact, I think I'll start by caching it OnStateEnter, but we could also use a registry of some kind to optimize further.
Unity is vast, so not everyone knows this, but you can add events to animations that import with the fbx. Just click the fbx asset, click the Animation button at the top of the inspector, then scroll down to Events and add your events to the timeline. Regardless, and as always, great video!
Thanks for the comment! That's a good thing to point out, but it still tightly couples your events to an Animation Clip. Your Animation Clip will always have the same events attached to it everywhere you use it. Separation of concerns is the main advantage with a system like this video.
It's a shame that we have to do such simple things ourselves, while Unity should have done it. I think the community has written more tools for Unity than the Unity itself. Btw nice video, I like how you implement such a useful things.
Could you extend on this topic and make a crossover with the Playable series? If I am not mistaken it should be possible to create a Scriptplayable with the functionality to call an Event on a specified Timing - the challenge here is to make the event previewable and "user friendly" to work with. because you dont have an animator controller window in the playable work-context.
Thank you! I think it would be better to have multiple events on event state component. That way more than one events on different part of the same animation can be fired. Or multiple components on same animation would do the same job?
i love it , it is very nice but i have one question what if i want to trigger same event multiple times in the same animation for example footsteps in walking animation
You can approach this in a few ways. The easiest will be to add an additional State Behaviour component for each event you want (so one for each step). Additionally, I would extend the editor to make the circuit breaker optional because your animation will probably be a looping one, so you could expose a checkbox to the inspector to allow for that and update the code slightly. Alternatively you can extend the component to handle multiple events at once, which isn't too challenging but remember to add some extra functionality to the preview to make it display correctly depending on which slider you are adjusting.
I use a lot of animation events in my game so i was really sicked to see this, sadly this doesnt really work when you use animator controller overrides and each character that overrides that animator has their triggers at different times... Would be cool to see a solution that covers this case
I used this approach as well - I liked it but it has 1 massive issue: It doesn't work with animator override controller, so I had to give it up and go back to putting events on animations directly. Unity has a very flawed animation system.
Love the channel. It has helped me a lot both with work and my own projects. There is one down side to this approach it ties the event to the Mecanim state. For most cases that is fine, but if you are dynamically adding your animations this dose not work. You can solve this with and make it compatible with your other one shot animation system by adding a "info" event to your animations and a empty "info" method to your animation controller script. Then add a ScriptableObject in the object field in the animation event. When the clip is played grab the object off the clip and use whatever data you need. This is useful for dynamic combo systems, or letting a controller know how much time they have to be in position for an attack/precise animation.
What about loop animations? Since we use hasTriggered, if the animation is looped like "spinning animation", if we put and event start and end of it, it will only work when the animation is changed to some other state.So it wont constantly invoke the event while the animation is in loop mode
To solve this; I have added previousValue, if we pass the threshold, i reset the bool value. public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { float currentTime = stateInfo.normalizedTime % 1.0f; if (activateOnLoop) { if (currentTime < _previousNormalizedTime) { _hasTriggered = false; } }
Nice. But I have question: What wil be if i try use few AnimationEventReceiver on same animator state? Will collision be with prevew buttons? In total, it is interesting solution.
Well, the AnimatorEventReciver goes on the GameObject in the scene, but you must mean the StateMachineBehavior class. This works fine with multiple instances, but if you enable 2 Preview buttons at once, only the first one you activated will show the preview in the Scene view. You could add some extra code that would disable the other buttons if you wanted, or just use them one at a time.
The animation preview doesn't work when I tried it with a synth character and with the RPG Tiny Hero Duo characters. It works fine when using the RobotKyle.
Well, at 100% there are no more frames in the clip to process. Can restrict the Range if that helps you, or even better would be to fire the event in OnStateExit if you need it to be at the very end of the clip (for non-looping clips)
This system is decent but it still feels meh, still one of the best solutions that we can do :( it really borthers me how unity didnt put some work in improving eventing during animations 😄 im curious how other engines deal with this, and if its better work flow
I'm not sure exactly what other engines do, but Unity's system of attaching their AnimationEvents as data within each AnimationClip does seem a bit short sighted. Unfortunately, it's probably also difficult for them to change now.
I want to associate events with animator states for an improved workflow where I have full control and am not locked into what's set in the animation itself - and I want it to be extensible to handle more options in the future.
The challenge comes when you have multiple events per animation. I am working on a hack and slash DMC like game. Each attack animation has VFX event, SFX event, Start/stop damage events, Start/Stop Invincible events at minimum. All of these can added in importer however they start to become a pain when they overlap one another.
And you can click on fbx file and add animation events thru it's inspector. And even select the event time by viewing animation. Or this method has some downsides?
For me, the main advantage of using this system over setting events directly in the animation import is that it allows separation of concerns by keeping event logic outside of the animation asset itself. For example, this allows you to reuse the same animation clip across multiple characters or states while customizing the events for each scenario, without duplicating or altering the original animation asset.
Happy Sunday! 🎉 Let's make an improvement on Unity's Animation Event system today! Check the repository for AnimationEvents from BlendTrees support as well.
This is the best unity channel on RUclips.
Thanks for the kind words!
word
Hey, I'm happy to see someone finally using StateMachineBehaviour! Would love to see you do a followup for everyone about extending ObjectPreview so those state machine behaviors can draw a preview of the animated character just like the Animator transitions do, without requiring modifying the scene character, and controlling that one's timeline.
Good idea. I actually thought about doing that too, but the video would have gotten a bit long. Something for the future!
@@git-amend Is this actually possible? I remember for one of the tools I made that I was not able to get a preview of an animation clip, ended up giving up in the end, it would have been a nice extra feature but if it actually a thing, I would love to go back and implement it!
@@X1N30F The preview window itself is doable with some effort. But, if you check the repository you'll find additional code for showing a blend tree animation in scene view that uses the Playable Graph (more functionality added after recording). Once you start making your own Playable Graph, you can do just about anything preview wise. Actually opening the preview window and using it requires using some reflection, but that window uses a similar technique.
@@git-amend thanks so much for getting back to me, I will definitely look into that and see how it goes! I might be able to just see what I could do to shoe in something for the preview window, since that's more so what I need rather than being in the scene view in any case I'm sure it will point me in the right direction.
I found it inconvenient to add animation events in AnimationEditor, so I created an animation wrapping class using ScriptableObject. I now see there's a way to add it directly to the state. Thank you for the helpful video.
You're welcome! Thanks for watching!
I'd also like to hear that how your SO object wrapping class is helping the situation, do you still use keys etc?
Honestly, having watched a ton of youtube tutorials over my gamdev career, I can't think of better educator than you! Well explained, to the point, easy to follow. A+ 👌
Wow, thanks! I appreciate the kind words!
Thanks Adam, This was quite full on but well worth the effort to go through! Cheers
You're welcome, glad it was useful!
git-amend reached into my brain and made a video helping with what I was currently struggling with.
Haha, nice glad to hear that!
I just discovered this channel, and the comments I see are very positive. I see why, bro. This right here is the truth. I am now a faithful subscriber!
Thanks so much! Welcome to the channel-I’m glad you’re enjoying the content!
I'm just watching the whole video in one gulp! Very clear, without water and at a high level! I can't wait for you to start coding shaders;)
Great to hear!
This is great! I always hated how Unity handled these animation events, not only setting it up was painful but also you had no way of knowing before hand if that clip had an event inside it. You could easily forget when changing animation clips for better ones actually to setup the events again. Storing it within the Mechanic State Machine is definitely a better approach.
Yes, my sentiments exactly!
Just starting to work on my first game in unity. Definitely a challenge with a full time job but your tutorials are fantastic!
Was just looking into animations as well 😊
Great to hear!
video is gold. appreciate everything you do for us :D
I appreciate that!
holy manoly, these was the one I needed just as this moment
Nice!
perfect for what im doing keep it up, unity needs to hire you, best tutorials around
I appreciate that! Maybe one day they will!
Great content as always! Thank you for consistently creating such amazing videos every week!
My pleasure!
Definitely the best channel for those who want to learn intermediate and high level developing skills!
One thing I don't like about unity's state machine behavior is that, if there are two states blending between each other, the second state's OnStateEnter will actually be called before the first state's OnStateExit, this can cause bugs when script logic is highly depending on the animation's begin and end. (luckily, thx for git-amend's better state machine video, I extend that one to split the game logic and animation, really save my life)
Thanks so much for the comment! You're absolutely right-Unity's state machine can sometimes cause issues with overlapping `OnStateEnter` and `OnStateExit`, which is why splitting game logic from animation, as you've done, is such a great solution for avoiding those bugs. Thanks for sharing that!
Brilliant as always !
Thank you! Cheers!
This is fantastic and very timely in my case. This week, I've literally been going though and doing all those annoying tasks to make animation events manually.
Awesome, glad to hear that!
Your channel is a godsend. Thank you for sharing your expert knowledge.
Thank you so much for the kind words! I’m really glad the content is helping you out-more great stuff is on the way!
Thank you so much for this, cumbersome is too polite a word I would use for Unity's animation system. I used AnimatorStateInfo to keep track of when animations were at varying points for things such as attacks or when I wanted logic to transition animations because Unity's Animation Event system either was too buggy or didn't respond at all when you wanted split second triggers. Love the addition of the custom editor!
Thank you! I agree, AnimatorStateInfo is underrated for things like that!
Man, Christmas came early this year 😄Thank you!
No problem!
Thank you so much for teaching me this!
You are so welcome!
Thank you! Unity anim events in 2021 (what I'm using here) is a mess and this just saved my day!
Awesome, glad to hear that!
Cool and useful video - as always)
I tried this approach and discovered two interesting things:
1) The script can be slightly modified so that it takes all the sub state machines that are found in the main state machine - I played around a bit and it seems that something has worked out for me, which means that it will work for others as well)
2) I wonder what you are thinking of doing with the mixing trees, since several animations are used there and you can't add behavior to them, unfortunately...
But I'll say it again - very, very cool video - thank you!
Thanks for the comment! I'm going to continue developing the script to handle some other things - handling substates is a good idea. For the blend trees, instead of normalizing a time, we can use the normalized weight param, and that part isn't too tricky - but displaying a preview is a little more difficult since there is no exposed method we can use for that. It will likely have to depend on some reflection. If I come up with a good solution for that, I'll be updating the repository.
How did you modify the script? Unfortunately, most of my animations are in sub state machines and it wont work.
The epitome of excellence.
Thank you!
This is really helpful, i tried a lot of stuff to avoid using the default animation events but i never thought of this, StateMachineBehaviors are so overlooked
Thanks for the comment, I agree!
Nice! Reminds me a bit of timeline signals too. I remember doing something similar like this a few years ago. Basically invoke unity events whenenver a state starts / stops etc. Seeing you write the editor tool code for this is very useful btw. I write a lot of editor code but just as often I think urgh editor stuff is so convoluted and badly documented, thanks for the learning opportunity.
Nice, I haven't heard anyone mention signals in a long time! Connecting events to the states instead of the clips seems much more intuitive.
You about to have me redo my entire animation system
Haha! I hope that's not a lot of work!
@git-amend I've been using animation events on the fbx Importer. Setting up the timing has been a pain in the ass. This is going to be a pain and a lot of time to do it but it's definitely worth it
Just do a couple and see how it's working for you first!
I am grateful for all the information you've given me but I can't help but be discouraged at the same time.
You are insane. Every video I watch there is an ocean of information to take in. I was already satisfied at 4:50, but clearly you were not, and you went on to adjust InspectorGUI and HumanBodyBones. I didn't even KNOW that HumanBodyBones even existed. I didn't even know that I wanted all this before you showed it, and now it feels like a requirement.
I thought knowing couple of common design patterns and a decent understanding of SOLID was enough for me to code efficiently, but there is so much knowledge out there that I don't know.
If I'm trying to break into the video games industry, is this the level of Unity/C# (in your other videos) understanding I need to be competitive? Have I been doing nothing the past year?
panik
Just keep in mind that the career of the game dev / software engineer is one of constant learning. You (and I) will never know everything, so just keep on learning new things every day! There is always more...
@@git-amend Will do... Just in case I didn't make it known enough, thank you so much for these high quality videos.
I didn't know functional programming existed in C# until you showed it in your channel.
I didn't know that you could edit the inspector until you showed it in your channel.
I didn't know about the 'is a' / 'has a' relationship for composition vs inheritance until you showed it in your channel.
Even though I am not at the level of mastery to fully understand/utilize all these concepts, this is the most helpful Unity channel for me BECAUSE it shows me what I don't know. I wish the best for you.
I wish I'd known about this before I made my "footsteps" video in my Music and Sound Effects series. I did my events the old-fashioned way :)
Ah well, always more than one way to skin a cat!
This is incredible! I was wondering if you had any ideas on how to handle Animation Override Controllers, and overriding the animation events to work with this set up?
That is a problem I haven't tackled yet. If I do, I'll make a video about it (or update the repository, make sure to enable notifications on the repo updates).
So useful feature, thanks))
You're welcome!
StateMachineBehaviors are pretty convenient when you remember they exist. I used them to make Animator play Timeline clips before I switch to Animancer plugin.
I wish Unity just improved Timeline editor so it works more in line with Animator - support editing multiple timeline clips, not requiring Playable Director, being more convenient in general.
Timeline allows to do much more things in more convenient ways, including stuff like in this video. Can wrap your read-only animation clips in timeline clips and it will work just fine. I hope new Dots animation system will solve this issue.
Hm, very interesting. I hope the same for the DOTS animation system.
Great idea and execution!
Thank you! Cheers!
Nice, just discovered the channel and really enjoyed watching the videos. One thing I noticed in the latest 2 tutorials is that the top play bar inspector is changed. I tried to do that once but everywhere it was saying that specific editor part is locked to modification.
Awesome, glad to hear that. Check out the Time Scale Toolbar Free asset in the video description. If you download that, first of all it's great to use, but it also has an Editor script you can analyze called 'Toolbar Extender' to start making your own customizations.
@@git-amend Thanks will check it out
You could've sold this as an asset for like 39.99 but not only you are giving it for free & and explaining every step... You are incredible man, to this community. I admire you. What would we do for eventName variable without strings? Since strings are easily mistyped, maybe enums? Or AnimationEventData SO reference instead of strings so that we can use it its name? Would it be overengineering.. IDK. Btw i couldnt see what we exactly changed on Animation Event property drawer. Seemed same to me as visually on the end.
very helpful, thank you
You're welcome!
Nice!
Thank you! Cheers!
would this solution be something that could go into the unity-utils? the animator is a built in to unity.
@@damonfedorick I think I will keep this stand alone, at least for now. There are several improvements that can still be made: handling Animator Overrides and Layers, as well as support for passing parameter values as well. And possibly handling blend trees.
@@git-amend are you hoping for Open source to handle those new features? or will you end up re-covering it in another video ?
Haven't decided yet. The parameters is easy, so is layers. Overrides and Blend trees will take some doing.
This is great, though there's a caveat that in some scenarios, events being tied to a specific animation file is actually _very_ useful. You can use one animator with several different NPCs with different animations, or like in my case, I've made a generalized sub-animator layer for handling weapons and items use, and what and when they activate their effects or sounds is determined by the animations they use... With script method you'd need to bake every single option into the animator, and clone animator for every new NPC, which immediately will make making edits to the animators a pain.
My only gripe with the stock Animation Events is that they're gone when you re-import the animation.
I wonder if it's possible to move this custom list of animation events into a separate external scriptable object and make it a dictionary of the animation files?
That's an interesting scenario. And also good to point out that they're gone after re-import. An approach of having a registry or dictionary might be one way to solve that, I'll have to give it a try as more features are added.
Nice. Definitely better than the vanilla animation event system!
Thanks!
Nice video!
May I request one on authoring cutscenes (Playable Director + timeline?). It has been a pain int the ass when it uses navmeshed and animator controller
Thanks for the suggestion! I’ll keep that topic in mind-working with NavMesh and Animator Controllers in Timeline certainly has its challenges, and a walkthrough could be helpful.
@@git-amend Nice! That would be awesome!
There is an official repository where there are examples for this. Sadly, the timeline API it uses is badly documented (or not enough "straight to the point" for my ADHD :'D)
As others have pointed out, the Unity Animation Event system is hard to work with and is immensely cumbersome when animation clips need to be updated. Your solution seems fantastic and Unity should pay attention.
I'm curious about the efficiency of this approach as the number of event state behaviors on a clip increases and the number of animated objects that handle events like this increases. Do you have any insight on if your solution is more or less efficient at runtime?
Thanks for the comment. This approach should be highly efficient at runtime for most use cases. As the scale increases, especially with hundreds or thousands of objects, it may require further optimization, but it’s unlikely to introduce significant performance issues in smaller-scale projects. I'll continue to optimize it as I think of improvements, or others suggest them. The only real cost is getting a reference to the Receiver component, which is minimal but could be optimized. In fact, I think I'll start by caching it OnStateEnter, but we could also use a registry of some kind to optimize further.
Unity is vast, so not everyone knows this, but you can add events to animations that import with the fbx. Just click the fbx asset, click the Animation button at the top of the inspector, then scroll down to Events and add your events to the timeline. Regardless, and as always, great video!
Thanks for the comment! That's a good thing to point out, but it still tightly couples your events to an Animation Clip. Your Animation Clip will always have the same events attached to it everywhere you use it. Separation of concerns is the main advantage with a system like this video.
@git-amend your approach and implementation is very much appreciated.
It's a shame that we have to do such simple things ourselves, while Unity should have done it. I think the community has written more tools for Unity than the Unity itself.
Btw nice video, I like how you implement such a useful things.
Thanks! You might be right!
By Watching this Video i am through i am doing wrong with animation events
Thank you Soo much
Awesome! So happy the video helped you out! Keep animating and have fun with it! 🎉
Could you extend on this topic and make a crossover with the Playable series?
If I am not mistaken it should be possible to create a Scriptplayable with the functionality to call an Event on a specified Timing - the challenge here is to make the event previewable and "user friendly" to work with. because you dont have an animator controller window in the playable work-context.
Not a bad idea. I'll look into what's possible. Thanks for the comment!
Thank you! I think it would be better to have multiple events on event state component. That way more than one events on different part of the same animation can be fired. Or multiple components on same animation would do the same job?
Feel free to extend this example any way you see fit!
Cant you just add another component? Since both component will call the receiver method in different time, it would still notify.
i love it , it is very nice but i have one question what if i want to trigger same event multiple times in the same animation for example footsteps in walking animation
You can approach this in a few ways. The easiest will be to add an additional State Behaviour component for each event you want (so one for each step). Additionally, I would extend the editor to make the circuit breaker optional because your animation will probably be a looping one, so you could expose a checkbox to the inspector to allow for that and update the code slightly.
Alternatively you can extend the component to handle multiple events at once, which isn't too challenging but remember to add some extra functionality to the preview to make it display correctly depending on which slider you are adjusting.
I use a lot of animation events in my game so i was really sicked to see this, sadly this doesnt really work when you use animator controller overrides and each character that overrides that animator has their triggers at different times... Would be cool to see a solution that covers this case
Yes, someone else mentioned this. I'll have a look and see what can be done.
@@git-amend Does the recent change made to the git repo address this "animator controller override" issue at all?
I used this approach as well - I liked it but it has 1 massive issue: It doesn't work with animator override controller, so I had to give it up and go back to putting events on animations directly.
Unity has a very flawed animation system.
Interesting, I'll have to look into that a bit more.
Love the channel. It has helped me a lot both with work and my own projects. There is one down side to this approach it ties the event to the Mecanim state. For most cases that is fine, but if you are dynamically adding your animations this dose not work. You can solve this with and make it compatible with your other one shot animation system by adding a "info" event to your animations and a empty "info" method to your animation controller script. Then add a ScriptableObject in the object field in the animation event. When the clip is played grab the object off the clip and use whatever data you need. This is useful for dynamic combo systems, or letting a controller know how much time they have to be in position for an attack/precise animation.
What about loop animations? Since we use hasTriggered, if the animation is looped like "spinning animation", if we put and event start and end of it, it will only work when the animation is changed to some other state.So it wont constantly invoke the event while the animation is in loop mode
To solve this; I have added previousValue, if we pass the threshold, i reset the bool value.
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
float currentTime = stateInfo.normalizedTime % 1.0f;
if (activateOnLoop)
{
if (currentTime < _previousNormalizedTime)
{
_hasTriggered = false;
}
}
if (!_hasTriggered && currentTime >= triggerTime)
{
NotifyEvent();
_hasTriggered = true;
}
_previousNormalizedTime = currentTime;
}
Seems like a good solution.
Nice. But I have question:
What wil be if i try use few AnimationEventReceiver on same animator state? Will collision be with prevew buttons?
In total, it is interesting solution.
Well, the AnimatorEventReciver goes on the GameObject in the scene, but you must mean the StateMachineBehavior class. This works fine with multiple instances, but if you enable 2 Preview buttons at once, only the first one you activated will show the preview in the Scene view. You could add some extra code that would disable the other buttons if you wanted, or just use them one at a time.
The animation preview doesn't work when I tried it with a synth character and with the RPG Tiny Hero Duo characters. It works fine when using the RobotKyle.
Try commenting out 'AnimationMode.StopAnimationMode();' in the PreviewAnimationClip, maybe it's getting turned off prematurely.
I get an error, "the playable graph is null." Why?
The worst thing about Unity animation events is the use of magic strings. I think it's better to replace them with enums.
Yes, not a bad idea at all.
Cool thing, but events didn't invoke when triggerTime is 1 or very close to 1
Well, at 100% there are no more frames in the clip to process. Can restrict the Range if that helps you, or even better would be to fire the event in OnStateExit if you need it to be at the very end of the clip (for non-looping clips)
This system is decent but it still feels meh, still one of the best solutions that we can do :( it really borthers me how unity didnt put some work in improving eventing during animations 😄 im curious how other engines deal with this, and if its better work flow
I'm not sure exactly what other engines do, but Unity's system of attaching their AnimationEvents as data within each AnimationClip does seem a bit short sighted. Unfortunately, it's probably also difficult for them to change now.
how about create event in Animation importer? already preview there. And no need to do any extra work
I want to associate events with animator states for an improved workflow where I have full control and am not locked into what's set in the animation itself - and I want it to be extensible to handle more options in the future.
The challenge comes when you have multiple events per animation. I am working on a hack and slash DMC like game. Each attack animation has VFX event, SFX event, Start/stop damage events, Start/Stop Invincible events at minimum. All of these can added in importer however they start to become a pain when they overlap one another.
And you can click on fbx file and add animation events thru it's inspector. And even select the event time by viewing animation.
Or this method has some downsides?
hmmm... what's the advantage of this over just going to the import of the animation and setting the event there?
For me, the main advantage of using this system over setting events directly in the animation import is that it allows separation of concerns by keeping event logic outside of the animation asset itself. For example, this allows you to reuse the same animation clip across multiple characters or states while customizing the events for each scenario, without duplicating or altering the original animation asset.
@@git-amend fair enough! I do like the fact that there are no magic strings to deal with if you set it up correctly
Nice job !
Thank you! Cheers!
@@git-amend Hi , could you check why my previous comment didn't appear !
No comments have been held for moderation, so I'm not sure...
@@git-amend That is weird ! Thank you for cheking