Injured Animations using Layers (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 use animation layers in unity to transition to injured animations as the health of the player decreases okay we'll start with a basic 3d scene that just contains a green plane for our character to stand on for the animations and characters we'll head over to mixamo.com first of all we'll go to the characters tab and select one of the characters then we'll click download we'll select fbx for unity in the format drop-down then we'll download the character to the assets folder of the project then we'll switch to the animations tab we'll search for an idle animation select this one then we'll click download and select without skin to just download the animation without the character we'll click download and save it to the assets folder of our project next we want an equivalent injured version of this animation we'll search for injured idle we'll select this one and download it the same as before next we'll look for a running animation we'll select this one for this demonstration we're going to have the character run on the spot so we'll tick the in place check box we'll then download this to our assets folder then we'll look for an injured running animation we'll select this one we'll tick the in place checkbox and download it back in unity we'll drag the character onto the hierarchy you'll notice that the textures are missing at the moment this is because they're embedded in the fbx file we can extract them by clicking on the asset then on the materials tab and then on extract textures we'll create a textures folder to extract to we'll get a pop-up regarding an issue with a normal map which we can just click on fix now to resolve now we need to configure the animations to loop endlessly we'll select the idle animation in the assets panel then we'll select the animation tab we'll tick the loop time and loop pose checkboxes and then click on apply we'll do the same for all the other animations next we'll create an animator controller for the character to do this we'll click the plus button in the asset panel and select animator controller we'll name this character animator controller we'll assign this to the character by dragging it onto the character in the hierarchy then we'll double-click the animator controller to open the animator window we'll right-click and create a new empty state we'll select this and rename it idle then we'll set the motion value to the neutral idle animation so now when the game starts it will play the idle animation we'll right-click again and add another state we'll name this one run and select the fast run animation we want to transition between idle and running after a period of time to do this we'll right click on the idle state and select make transition we'll then click on the run state to create the transition if we click on this transition we can see that it has an exit time after 97 of the animation has played we'll leave this as it is and create a transition back the other way for this one we'll increase the exit time to 10 to have the run animation last longer next we'll add a new layer for the injured animations we'll call this injured we want this layer to have the same states and transitions as the base layer but make use of the injured animations to do this we'll click the cog icon and then tick the sync checkbox this layer is now synchronized with the base layer and has the same states and transitions we'll select the idle state and set the motion to the injured idle animation then we'll set the animation on the run state to the injured run let's press play to see this in action the character starts in idle then they transition to running and then they transition back to idle at the moment the injured layer is not having any effects we'll dock the animator window alongside the game window now we can click the cog icon on the injured layer and move the weight slider we can see the character now transitions from healthy to injured and back again as we change the weight of the injured layer in a moment we're going to look at how we can control this weight from a script increasing it as the health of the character decreases before we do this though we'll stop the game and add a text ui object to display the health of the character to do this we'll click the plus button in the hierarchy and then select ui text text mesh pro when prompted we'll import the text mesh pro essentials we'll name this health text then we'll make some changes to the position and formatting of the text we'll click on the anchor button we'll hold down shift and alt and then click on the top right button this will anchor the text to the top right of the screen we'll set the width of the text to 300 and we'll set the y position to -10 we'll also make the text bold now we'll add a script to do this we'll click on the character in the hierarchy then we'll click add component and add a script component we'll name this health controller then we'll double-click the script to open it in the editor first of all we'll add a serializable field for the health text we'll need to add the tm pro namespace for this then we'll add a float field for the maximum health of the player we'll just set this to 100 we'll also add a field for the current health in the start method we'll set the current health to the maximum next we'll add a field for the animator in the start method we'll get the animator component and assign it to this field to be able to change the weight of the injured layer we need to get the index of this layer we'll add a field for the index then in the start method we'll get the layer index from the animator in the update method we'll check if the spacebar has been pressed if it has we'll reduce the current health by 10 of the maximum we'll check if the health has dropped below zero normally this will be the end for the player but for this demo we'll set it back to the maximum then we'll calculate the percentage of health that's remaining by dividing the current health by the maximum we'll set the text on the ui to show the current health percentage then we'll calculate the target layer weight which will be 1 minus the remaining health percentage we'll set the layer weight on the animator now as the player's health decreases the weight of the injured layer will increase let's save the script and switch back to unity we'll drag the text object into the health text field in the inspector let's press play to try this out now when we press the space bar the health of the player decreases and we can see the animations changing the transitions are a bit too noticeable at the moment though let's stop the game and see how we can make it a bit smoother in the update method we'll get the current weight of the injured layer then we use the smooth damp function to smoothly transition from the current weight to the target smooth damp requires a reference to a velocity field to keep track of the speed of the transition so we'll add this now we'll set the smooth time to 0.2 seconds let's save this and switch back to unity to try this out now the transition from healthy to injured is much smoother the final injured animation is quite extreme though and we may want a less prominent limp let's stop the game and switch back to the script to look at how to do this what we're going to do is add the capability to set a maximum weight for the injured layer we'll add a field for the maximum injured layer weight then in the update method we'll multiply the target weight by this maximum let's save the script and switch back to unity we'll set the maximum injured layer weight to 0.5 and press play to try this out now when we decrease the health the injured animation is much less extreme okay that covers everything for this video a big thank you to all our amazing patrons for 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: 20,380
Rating: undefined out of 5
Keywords: Animation Layers Unity, Injured Animation Unity, Animation Layers Unity3d, Injured Animation Unity3d, SetLayerWeight Smooth Unity, SetLayerWeight Smooth Unity3D, SmoothDamp Unity, SmoothDamp Unity3d, Unity, Unity Tutorial, unity3d, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: 5LPEsnzmxNk
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.