How to Animate Characters in Unity 3D | Animation Transitions With Booleans

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Great stuff as always :) Really enjoyed how you broke this one down - I feel like I'm getting a feel for how this stuff actually works

👍︎︎ 1 👤︎︎ u/ajax8888 📅︎︎ Jul 10 2020 🗫︎ replies

Subscribed! You have a nice speaking voice that comes well across video. I'm excited to learn more about Unity. I know this one is just about character animation, but I'm also interested in a general introduction and beginner level stuff if you take on those as well.

👍︎︎ 1 👤︎︎ u/tallsy_ 📅︎︎ Jul 10 2020 🗫︎ replies
Captions
hi I'm Mickey welcome to my channel about learning game development this is a video in a series dedicated to learning unities animation system in the previous video we broke down the five properties that make up you MVC animator component in order to understand how they all work today we're gonna take a deeper look at one of those five components the animator controller by the end of this video we'll understand one of the simplest ways we can transition between animation States boolean values before we get going if you're enjoying the series so far please consider subscribing to the channel alright let's get started to ensure that everyone is up to speed we've left off the last video by setting our default animation state to our downloaded running state when we press play our character will continuously loop the running animation now getting our character to simply use the running animation was great but wouldn't it be awesome if we could have control over the animations the parameters tab in the left pane of the animator controller will help us do just that let's switch back to having idle as the default state and have a look at the parameters when first opening this tab you will have text saving the list is empty to add a parameter let's press the plus sign in the top right here we're presented with the four types of parameters that we can use to control the state machine float in bool and trigger a float is a number with a decimal and in is a whole number a boolean means true or false and the trigger is also true or false but unity is programmed to handle a trigger a little differently than a boolean today we're gonna focus on using boolean s-- let's start by adding a boolean parameter and renaming it to is walking well expect our is walking boolean to be true when a player presses forward and to be false when they stop so at this point our is walking boolean does nothing pressing play will just loop the idle animation how do we get from idle to walking we can start by connecting the two animation States with what's known as a transition right click on the idle animation and select make transition then press on the walk animation state to connect them let's press play to see what happens we'll notice that the idle animation still plays but if we wait long enough our character transitions to the walk animation so how can we use the boolean that we created to control which animation is currently active let's press on the transition and look at the inspector to see if we can find our answer to this question from top to bottom we'll see the name of the two animations tied at the transition toggles for solo and mute a space provided to rename the transition a toggle for has exit time drop-down menu titled settings a timeline consisting of two animations and finally a menu titled conditions let's start by renaming the transition to start walking and then navigate to conditions and press the plus button pressing this button will automatically create a new condition for the selected transition now our newly titled start walking transition will be dependent on the is walking boolean and in order for our care to actually begin walking our boolean and will need to be true but how do we grant our player control over the value that the boolean holds the answer is code in the hierarchy we'll select our Y bot character navigate to add component and type in script to be presented with a new script option let's select it and title our new script animation state controller because this script will be in control of our active animation state give it a second and a new script will attach itself to the Y bot well then double-click on the script to have our default IDE open so we can edit in our code if you don't have an IDE installed I personally use Visual Studio and we'll link to where you can download it in the description if this is your first time looking at code welcome that's super exciting by default we are always presented with two functions with the script generated by unity the start and the update function the start function will be called or cur one time when the game object that we have the script attached to is instantiated which in our case is immediately when pressing play the update function occurs every single frame that the game is running and the game object that the script is attached to is active like we said before we want to give players control over the boolean is walking now we know that ease walking is a parameter and parameters are stored in our animator component so our no script we just need to create a reference pointing to the same animator component attach the Y bot and we'll gain access to the boolean via code let's do it well first declare an animator reference variable so that when we search for the animator component we have a place to keep it next we'll use unities built-in getcomponent function which searches through the game object that our script is attached to for whatever component we pass in as a parameter in this case animator we'll do this in the start function so that we know that our animator reference variable will immediately refer to the animator the moment that our character is active and now that we have this reference we can use some of you unities built-in functions to switch our boolean value between true and false given a player's input in the update function we'll check for when a player press the W key meaning they want to move forward and our character should start walking using one of unities built-in functions input get key we can pass in W as a parameter so that when W is pressed down R is walking boolean should be true by accessing your animator reference we can use the set bool function and set is walking to true let's give this a shot at first glance it would appear that our code didn't work but if we wait long enough our character will begin walking but why the answer is simple returning to our transition will notice that the has exit time toggle is selected by default has exit time it means that a certain percentage of our active animation has to be completed before the transition is willing to move on to the next animation pressing on the settings drop down will display even more information about the has exit time option the first setting titled exit time is set with a default value of 0.9 seven this means that 97 percent of the animation has to be completed before the transition will occur explaining why your character waited before starting to walk let's toggle has exit time and we'll see that now when we press W our character will begin walking immediately we'll also notice that if we let go of W our character keeps walking at this point we're missing two things an additional transition and a little more code let's create the transition first by right-clicking on our walking animation state and connecting it to our idle animation state then toggle off has exit time and create a new condition by default our is walking boolean will be selected but this time we want the condition to be when the value is false because this transition is pointed in the opposite direction we are now able to lead our of animation state from the walking state back to the idle State however in play mode pressing W still results in our character indefinitely walking this is because the value of the parameter stored in the animator controller is never updated through our code so when we currently press W our code is setting the parameter true but when we release the key it stays true let's fix this beneath our previous code we'll right nearly the exact same conditional statement as before the difference now is that we're going to put an exclamation mark before the condition and we'll set the boolean value to false the exclamation mark means not so now you can interpret our code as if the player's not pressing W then we want to set the is walking boolean to false awesome before we leave our code let's quickly refactor or simplify our code for better performance we are reusing nearly identical conditional code here so we can save this value to a new variable so that you need does not need to execute the same function twice we'll say the boolean for word press is equal to input get key with W pass in as the parameter we can then replace a conditional logic with this new forward press variable and now the input cat key function is only performed once additionally we don't need to continuously set or is walking parameter to be true if it already is let's add an additional condition to make sure we're only setting the value when we need to we'll use animator get bool and pass in is walking as a parameter and we'll store that in a local variable of the same name in our if statements we can say if we're not walking and our player presses forward then we should set the value to be true and if we aren't walking and the player stops pressing forward then set the value to be false the last change we'll make to increase performance is to use the animator string to hash function by setting a variable of type integer we can replace our string parameters of is walking with the stored version of a simpler data type with our changes in place let's press play and see the results you'll notice now that when we press W our character moves and when we were released our character stops now that we know how to use transitions let's attempt to connect our running animation state we'll start by adding a new parameter we'll call this one is running well create a new transition to and from the animation state using the is running parameter lastly we'll remove the exit time and modify our code we'll start by creating a new variable similar to the forward pressed we'll call it run press well set this variable to the value of input get key with left shift of being the parameter this means that if a player is pressing left shift this boolean will be true next we'll duplicate our conditional logic from before but instead of just checking to see if the player is pressing the Run button we'll also check to see if they're pressing forward in this case we want both in for our character to stop running we now want to check to see two possible options if the player stops pressing the Run button or if they stop pressing forward we can also check to make sure that the player isn't already running before performing this logic we'll create a variable is running instead of equal to the animator get bool and pass in is running as a parameter next we'll add this new condition to our first if statement if the player is not running in both keys are pressed then set is running to be true and in our second if statement if the player is running and either the keys or release set it to be false and once again we'll use the animators string to hash function to simplify our code and to increase performance awesome now we have easy control over all three of our animation states before we end today's video let's go back and have a look at some of the other settings available for transitions as previously mentioned we have toggles for solo and mute mute we'll disable a transition completely so even if you are walking and attempt to run the walking state will remain the active state whereas solo will only play this transition we can demonstrate this by disconnecting the transition from walk to run and create a new transition from idle to run entering play mode will notice that our character will no longer walk by pressing W and if we attempt to run by pressing both W and our left shift the animation will fly let's take a look at transition duration well notice how if we set the property to zero the animations become significantly snappier where's a longer transition we'll add additional blend frames between the two animations transition offset determines where we want the animation to start from a zero to 100% basis normalized to a zero to one value so if we want to start 25% of the way into an animation we would use point two five the last setting we'll talk about today is interruption interruption allows you to switch animations mid transition so if we are currently in the idle state and we're switching to the walking stay and we have a long transition between the two this setting would allow us to switch to the running state mid transition and this would only work if we chose the next state as the option because we're going from the current state of idle to the next state of walking and our running animation state is connected to the walking animation state if we set the interruption to none the full transition will have to take place similarly in our example the current state is idle and we don't have a direct transition between idle and running so if we select the current state option the full transition will have to play in the next video we'll take a look at an alternative way to connect our animations using flow parameters and blender ease and as the series continues we'll learn to retarget our animation to our own characters and get them moving in 3d space as always if you learned something new today please consider subscribing to the channel it really helps me out and lets me know that we're headed in the right direction that's all for today thank you for watching and I'll see you in the next video [Music]
Info
Channel: iHeartGameDev
Views: 116,666
Rating: undefined out of 5
Keywords: how to animate in unity, how to animate in unity 3d, unity animation transition, How to Animate Characters in Unity 3D, animate characters unity, unity3d animation, animation transitions unity, unity booleans, animation transitions using booleans, player controlled animation, unity for beginners 2020, nicky b game dev, animation transitions, unity control animation from script, animate in unity, How to Animate Characters in Unity 3D | Animation Transitions With Booleans
Id: FF6kezDQZ7s
Channel Id: undefined
Length: 11min 59sec (719 seconds)
Published: Sun Jun 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.