How to use Animation Transitions (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how we can easily control animation transitions from our scripts if you find this video useful please don't forget to click like and subscribe and if you'd like to support our work and get access to the source code you can find us on patreon okay we'll start with this scene that has a character and some obstacles the character has a character controller component attached that is used to move the character around and handle collisions with the obstacles we've also set up a cinemachine camera to follow our character around we created this scene in the earlier videos of our 3d platformer series so take a look if you want to know how it was done at the moment our character isn't animated and just glides around the scene we're going to replace this with an animated character from miximo miximo is a free library containing thousands of character animations we have a dedicated video on this that goes into detail on all the available options if you want to know more first of all we'll go to the characters tab we'll scroll down to the bottom and choose this character called doozy we'll click download in the format drop down we'll choose fbx for unity and then we'll click download we'll navigate to the assets folder of our project and save the file there if we switch back to unity we can see the character has been loaded into the assets now we can replace our original character with this one before we delete the original let's have a look at the components attached to it it has a character controller and our player movement script we'll need to remember to add both of these to our new character let's delete the original from the hierarchy then we'll drag the new character into the hierarchy at the moment you'll notice that we don't have any textures on our character this is because they are embedded in the fbx file to extract them we'll click on the asset click on the materials tab in the inspector and then click on extract textures we can then select a folder to extract to we'll get this pop-up regarding an issue with a normal map which we can just click on fix now to resolve now we have our new character we need to allow the player to control it we'll click on add component and add our player movement script back in we'll set the movement speed to 5 and the rotation speed to 720 we'll leave the jump speed at zero as we're not going to animate the jump just yet we'll click on add component again and search for the character controller component we'll set the center to 0.9 on the y-axis and we'll set the height to 1.7 we also need to update our camera to follow the new character we'll click on our virtual camera in the hierarchy then we'll drag the new character into the follow slot while we're here we'll also change some of our camera settings to get a better view we'll change the rotation to 20 on the x-axis and we'll zoom in a little by setting the camera distance to 5. let's press play to try this out now we have our new character moving around in our scene the next step is to animate the character to make the movement look more natural we'll stop the game and switch back to miximo to download some animations when our character is standing still we want to play an idle animation we'll click on the animations tab and search for idle we'll scroll down and select the breathing idle animation we'll leave all the settings as they are and click download we don't want the character to be included in the download so we'll select without skin then we'll click download and save to our assets folder if we switch back to unity we can see the animation has loaded in our assets by default the animation is set to just play once and then stop we want our animation to loop forever so we need to tick the loop time checkbox in the inspector we'll also check loop pose which will ensure the animation loops seamlessly we need to click the apply button to save these changes to add the animation to our character we'll expand the asset then we can just drag the curved triangle animation icon onto the character in the hierarchy if we click on the character in the hierarchy and look in the inspector we can see that it now has an animator component an animation controller has also been created and assigned if we double click on this it will open the animator window which shows our animation states this is saying that on entry the idle animation will play let's press play to try this out the idle animation will now play repeatedly our character looks much better when standing still but we now need to transition to a running animation when moving let's go back to miximo and search for a running animation we'll select this animation in the preview window we can now see our character running at the moment it runs off into the distance and then loops back to the start if we check the in place checkbox it will change to running on the spot this is what we want as we're going to control the movement from our script it is possible to have the animation actually drive the movement using a feature called root motion we'll have a look at this in a future video but for now we'll keep it simple let's click download and save it to our assets folder back in unity we'll select the new animation asset and set it to loop as we did previously we'll then expand the asset and drag the animation onto the character if we switch back to the animator tab we can now see a box for our running animation next we need to tell unity that we want our character to be able to transition from idle to running to do this we'll right click on the idle state and select make transition we'll then click on the running state to link them together let's click on the transition and expand the settings section in the inspector at the moment the transition is set to have an exit time this means that it will transition from idle to running after a certain amount of time has passed the exit time is in normalized time rather than real time this means that the exit time value represents the percentage of the animation that has played so at the moment we will transition from idle to running after about 97 of the idle animation has played the transition duration is how long it will take to smoothly transition from one animation to the other this is in real time that can be switched to normalized time by unticking the fixed duration checkbox so at the moment our character will start in the idle state after 97 of the animation has played it will then take 0.25 seconds to change over to the running animation let's press play to see this in action now our character smoothly transitions from idle to running the next thing we need to do is control when this transition happens from within our script let's stop the game and switch back to the animator we're going to change the transition from having an exit time to being triggered by a boolean parameter we can add a boolean parameter by clicking the plus button in the left hand panel and selecting bool we'll call this parameter is moving now we can use this parameter as a condition for triggering the transition we'll click on the transition and uncheck the has exit time checkbox then we'll click on the plus button in the conditions section this has added the condition we want when the is moving parameter is set to true the character will transition from idle to running we also want to do the reverse and transition back to idle when the character isn't moving let's right-click on the running state and add a transition back to idle we'll uncheck the house exit time checkbox and add a condition this time we'll change it to trigger when is moving is false now that we've got our transition set up we need to set the animation parameter at the correct times in our script the first thing we need to do is get access to the animator component we'll add a private field for this and we'll get it in the start method we can then use the setball method to set our parameter in our update method we already have an if statement that is checking whether the character is moving if it is we'll set the is moving parameter to true on the animator we'll then add an else statement and set it back to false when the character isn't moving let's save the script and switch back to unity to try it out now our character starts and stops running at the correct time in future videos we'll look at how we can add animations for jumping and falling and how we can improve our transitions further with animation blend trees ok that covers everything for this video hope you found it useful please leave any questions or feedback in the comments and don't forget to subscribe so you don't miss the next one thanks guys
Info
Channel: Ketra Games
Views: 165,841
Rating: undefined out of 5
Keywords: Animation Transitions, Run Animation, Animation Transitions Unity, Idle Animation, Animator SetBool, Animator Transition Condition, Unity, Unity Tutorial, unity3d, development, programming, coding, C#, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D
Id: 2_Hn5ZsUIXM
Channel Id: undefined
Length: 9min 58sec (598 seconds)
Published: Sat Apr 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.