Add Random "Bored" Idle Animations to Your Character (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 add a bit more personality to a character by playing random animations after they've been left idle for a period of time okay we're going to start with this scene that has a character that starts off in an idol state from this state the character can run or it can jump we've covered how to create this scene in the earlier videos of our 3d platformer series so take a look if you want to know how it was done alternatively you can download this project by supporting us on patreon all the relevant links can be found in the description okay so the first thing we need to do is get some animations to play when the character is bored for this we're going to head over to mixamo.com first of all we'll go to the characters tab and select the character we've been using called doozy then we'll go to the animations tab we'll search for board we'll select this one and download it we need to remember to select fbx for unity and without skin to just download the animation download this to the animations folder in our project to make it more interesting we'll download a couple more animations we'll search for offensive idol and download this one then we'll search for shuffling and download this one back in unity we need to change some of the settings on the animations we'll navigate to the animations folder in the assets panel we'll select one of the new animations and go to the rig tab in the inspector we'll change the animation type to humanoid for avatar definition we'll select copy from other avatar and choose the character avatar as the source we'll click apply to save the changes then we'll go to the animation tab we'll click loop time and loop pose to have the animation loop forever as we don't want the animation to affect the position or rotation of the character we'll bake all the transforms into the pose we'll also set all the based upon values to original to use the original values in the animation if any of this seems unfamiliar then take a look at our video on root motion where this is covered in more detail we'll click apply to save the changes then we'll do the same for the other board animations next we'll go to the character animator to look at how we can fit these new animations in we could create a new state for each one but then we'd have to add transitions from all these states to running and jumping and it would end up looking something like this this is not the easiest to maintain so what we're going to do is use a blend tree instead we'll right click on the idle state and click create new blend tree in state this will convert the state to a blend tree we'll rename this state to give it the more generic name of idle then we'll double-click to open it up usually a blend tree is used for blending similar animations such as walking and running here we're going to use it to blend between the different idle animations we'll click on the blend tree and then add four motions we'll set the first one to be the default idle animation then we'll add in the three board animations we'll go to the parameters tab to add a parameter for controlling which animation is playing we'll click the plus button and add a new float parameter we'll name this board animation then in inspector we'll select this new parameter the blend tree will blend between the different animations based on the value of this parameter and the threshold set if any of this seems unfamiliar then take a look at our video on blend trees where this is covered in more detail we'll deselect automate thresholds so that we can set them manually we'll set the thresholds from 0 to 3. let's press play in the preview to see how this will work when the parameter is at 0 the default idle animation will play if we set this to 1 then the first board animation will play if we set it to 2 the second will play and if we set it to 3 the third will play now we need a way to control the setting of this value to do this we're going to add a behavior script to the state we'll go back up to the base layer and select the idle state then we'll click on add behavior and create a new script we'll name this board behavior we'll go to the root in the assets panel and drag this into the scripts folder then we'll double-click the script to open it in the editor there are various methods in here that we can use but we're just going to need on state enter which is called when the animation state starts and on state update which is called every frame when we're in this state we'll create a private float field for the time until board we'll make this serializable so that it can be set in the inspector then we'll add a private serializable int field for the number of board animations we have to keep track of whether the character is currently bored we'll add a boolean field named isboard to know whether the character has been idle for long enough to become bored we'll add a float field to store how long the character's been idle then we'll go to the update method and check that we aren't currently bored if we aren't then we'll increase the idle time by time.deltatime next we'll check if the character has been idle for long enough to become bored if it has we'll first set isboard to true then we need to choose one of the board animations to set to do this we'll generate a random number within a range we'll set the minimum value to 1 and the maximum value to the number of animations plus 1. we need to plus 1 as the maximum value we pass into the range is not included now we can set the value of the animator parameter this will set the blend tree to play the random board animation next we need to switch back to the normal idle animation after the board animation has finished we can do this by adding an else if statement in here we'll use the state info to check the normalized time of the animation this will let us know how far through the animation we are zero would indicate the start of the animation and one would indicate the end as the animation is looping though the value will go up to two at the end of the second loop and three at the end of the third to find if we're towards the end of a loop we can do modulus one this will divide the value by 1 and return the remainder if this remainder is greater than 0.98 then we know we're near the end of one of the board animation loops if this is the case we want to reset back to the default idle animation we'll create a method to reset everything we'll take the animator as a parameter in here we'll set isboard back to false idle time back to zero and we'll set the animator parameter back to zero to go back to the default idle animation we'll call this method from our else if statement we'll also call it from on state enter to reset everything whenever we enter the idle states let's save this and switch back to unity we'll set time until board to 3 seconds and we'll set the number of board animations to 3. let's press play to try this out if we wait we'll see the different board animations playing at random now we have the basics working but we need to improve the transitions as they're really noticeable at the moment let's stop the game and switch back to the script to do this first of all we'll change it to only switch to the board animation if we're at the beginning of the animation this will stop us going into the board animation part way through and will look much better we'll add a condition to check the normalized time again this time we'll check if we're near the beginning of a loop by checking if the remainder is less than 0.02 next we want to smooth out the transitions between the different animations we'll create a field to store the animation we want to transition to we'll set this to the random number that we generate then rather than setting the animation instantly we'll move this line to the bottom of the method we'll set it to transition to the number stored in the field then we can use the damping functionality of this method to move to this value gradually frame by frame we'll set it to take 0.2 seconds to transition and we need to pass in time dot delta time for the damper in the reset method we'll remove the line that sets the animation parameter back to zero and we'll just set the field to zero instead then in the update method it will gradually transition back to this value we can remove the animator parameter from this method now as it's no longer needed let's save the script and switch back to unity to try this out we'll wait for the character to get bored again now the transitions are smoother and the board animation always starts at the beginning you may notice that some of the transitions still look a bit strange let's stop the game and go back to the animator to look at why this is we'll go to the blend tree and set it to the last animation from here it has to transition through all the animations back to the default idle that's why when it transitions we see a little bit of each one and it makes the transition more noticeable to fix this we're going to add a motion between each of the board animations we'll set these to the default idle animation now each board animation can transition directly to the default idle animation we need to make some changes to our script to support this to get the right board animation we now need to multiply our random number by 2 and -1 after we've chosen the random animation we'll set the animation parameter to the closest default idle animation we can do this by setting the animation parameter to be our desired animation -1 this will instantly set it to the closest default idle animation and will allow it to smoothly transition without passing over any other animations we also need to change the reset method we can't just set the animation number back to zero as that will transition through all the animations we just want to set it back to the closest default idle animation to do this we'll check if the character is currently bored if this is the case we'll subtract 1 from the animation number to go back to the previous default idle animation let's save the script and switch back to unity to try this out we'll wait for the character to get bored again this time the transition looks much better we'll wait for another one to be sure this one looks good as well okay that covers everything for this video a big thank you to all our patrons we really appreciate you helping to support the channel if you'd like to help and also get access to the source code you can find details in the description please leave any questions or feedback in the comments and subscribe and click the bell icon so you don't miss the next one thanks guys
Info
Channel: Ketra Games
Views: 26,434
Rating: undefined out of 5
Keywords: Random Idle Animation Unity, Bored Animation Unity, Play Animation When Bored Unity, State Machine Behaviour Script Unity, Random Idle Animation Unity 3D, Bored Animation Unity 3D, Play Animation When Bored Unity 3D, Unity, Unity Tutorial, unity3d, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: OCd7terfNxk
Channel Id: undefined
Length: 16min 3sec (963 seconds)
Published: Sun Apr 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.