Unity Nightmares - Animation duration
I have an animation. I want to play the animation on a mesh. For some reason, even this easy task is made complicated in Unity.
I have to create an Animator on my mesh, open it, setup a state for the animation, connect this state with the other states that I want to transition from, figure out stuff like “Exit Time” and “Transition Duration”, setup a condition for this transition to happen, for that I need a parameter, set it as the condition, then go into code, reference this animator, change the parameter and finally, my animation is playing.
The Transition and Condition part is too much overhead, I want to find a way around that while still having more or less smooth changes between animations.
For that, there is a nice little function called “Crossfade”. With it, I can just input a random state and it will transition to that state and play its animation. Tarodev made a very good and concise video about it:
Ok. Now the last thing: I want my animation to play for exactly 5 seconds. And this is where the pain started.
Apparently, this is not a Usecase that Unity was designed for. You can’t tell the Crossfade function to play the animation for 5 seconds. In fact, there isn’t even a way to directly set the duration of an animation within the Animator itself. You can change the playback speed, but if I wanted an animation to play for 5 seconds at first, and for 8 seconds a second time, I would have to create another parameter and connect it to the state’s speed, and change it whenever I want to play the animation. I would have to setup that parameter for every animation. On every Animator.
Trying to go the coding route, there is a handy little variable called “length” on an Animation. This tells you the number of seconds that this animation is playing. Animators on the other hand have a variable called “speed”. This multiplies the playback speed of the currently playing Animation, avoiding the need for a bunch of parameters.
So in theory, I would just take the length of the Animation, divide it by the number of seconds I want the animation to play, and put that into the speed variable of the Animator. Sounds relatively easy, right?
There is a catch however. I would have to get the Animation of the Animator State I want to Crossfade into.
I have searched everywhere around the web for a solution that solves this, but I haven’t found any. So I turned to ChatGPT. And it vomitted out this banger of a code:
Ok, as long as it works I guess? And it did work. I could now play my animations for as fast as I wanted, depending on the circumstances that I set up.
So I wanted to test it out on my phone. But the build failed.
Why did it fail? Because half of the classes in the Script were UnityEditor includes, which can’t be used on your target device. Great.
My final solution now is to still keep this script ChatGPT gave me, and use it to fill out a mapping between states and animation durations, and using that mapping ingame. The script will be stripped out at build time, and only the mappings are making it into the game. If you know a better solution, please do tell me, as I am angry myself that I had to make it this complicated.
Was all of this really necessary, Unity?
Get Skylite Heroes
Skylite Heroes
Conquer dungeons and find epic loot in this Dungeon Crawler Action RPG!
Status | In development |
Author | BrutDev |
Genre | Action |
Tags | 3D, Action RPG, Dungeon Crawler, Fantasy, mobile, Roguelike, Roguelite, Singleplayer, Top-Down |
Languages | English |
More posts
- Changelog v0.4Jun 04, 2024
- Version 0.4 released!Jun 04, 2024
- Progression and World MapMay 27, 2024
- Version 0.3 releasedMay 17, 2024
- Changelog v0.3May 17, 2024
- Changelog v0.2Dec 17, 2023
- Version 0.2 releasedDec 17, 2023
- The next updateDec 01, 2023
- Dungeon System PreviewNov 21, 2023
Leave a comment
Log in with itch.io to leave a comment.