Animations in Unity3D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
on this channel we've explored a ton of important concepts surrounding animating characters in unity last episode we learned to stack multiple animations together using animation layers today is all about the animations themselves let's take a deep dive into the animation tab animation curves and animation events my name's nikki this is i heart game dev you're here to learn so let's get started we'll start today with the fundamentals of animations in unity opening a new unity project we'll right-click and add a plane to the scene call it ground and for the sake of looking good we'll add a colorful material next we'll add a cube to the scene hierarchy now in the default window setup for unity the animation window is missing let's add the animation window by right-clicking on the area next to the console tab and select animation cool when we first look at the animation window we can see it is clearly divided into two main sections the menu on the left and the timeline on the right however the timeline currently states no animatable object selected making the animation window pretty useless let's quickly fix this by pressing on our cube we'll both see and press a button that will create a new animation we'll title this first animation and save we can now see the title of our selected animation in the menu section and most of the menu is no longer grayed out we also have a new option in the menu titled add property let's add a new property before we dive into each of the menu options pressing on add property will be presented with multiple options each option representing a component attached to our cube that has animatable properties this may come as a surprise because we typically just think of transforms when we think of animations like the position rotation and the scale of the game object however there are a lot of components to have animatable properties for example in a dodge animation we can animate the box collider of the game object so that it's disabled at the beginning of the animation and is enabled at the end of the animation but again this is just an example of other possible animatable properties for now we'll just select transform and position we'll notice that cube position was added to the menus list and on the timeline there are two new diamonds these diamonds are keyframes a keyframe stores the current value of the property at the time the keyframe's recording so in this case the cube has a stored value at both of these keyframes of 0 1 0 on the x y and z axes respectively because that's where this key was located when we added this property this can be seen in the transforms component of our inspector and if we press on the arrow in the menu list we can see the same detailed breakdown taking a look at the menu options we'll start with the top row when selected preview will display the animation in the editor as we scrub or move through the timeline it's indicated that we are in preview mode by highlighting the button in white and the timeline in light blue currently this isn't noticeable because the only two keyframes we have store the same value meaning our key just doesn't move but this brings us to our next setting the red record button pressing record activates both preview mode and record mode our timeline turning red any changes made will now create a new keyframe at the current frame and store the current value of the animated property if there is already a keyframe the value will be overwritten so be careful pressing on the 30 on the timeline let's test out recording we can do so by either dragging the cube to a new position in the editor or just by typing a new value in the transform position on the inspector we'll see a new keyframe added storing the newly set value we can click and drag keyframes to move them around and we can highlight multiple keyframes to either move them around together or to scale them closer together or further apart and this brings us to the next five buttons in the animation window which control the animation timeline the first and last will navigate the timeline to the start and end of the animation which happens to be the first and last keyframe the middle two buttons will navigate through the keyframes in the timeline one at a time and the middle button will play the animation the last menu item on this first row represents the current frame of the animation in the second row we will see a drop down menu which has our current animation selected here we have the option to select any other animations available to the currently selected game object or to create a new animation one thing you may have noticed is that there is an option titled samples and it is possible you do not have it to find this setting navigate to the three dots drop down on the far right of the timeline here we have multiple new options we have the option to switch between seconds and frames for our timeline we also have the option to toggle ripple which changes how moving keyframes around works with ripple activated the surrounding keyframes are impacted by the modification of the selected keyframes when active moving a keyframe to the left or right will also move the surrounding keyframes in the same direction and if we were to scale a selection of keyframes we'll see a similar effect next we have the toggle to show sample rate and a menu to set the sample rate directly pressing the show sample rate toggle will reveal the sample rate box in the menu the value in the samples box represents the number of frames per second that the animation is intended to run the animation will currently read at 60 frames per second if we change this value to 120 we'll see the playback speed doubles and at 30 it plays at half the speed the bullseye icon next to samples is filter by selection this allows us to only display the selected game objects in the timeline this is super useful in something like a complex character rig where we could be dealing with dozens of bones in an animation with this option active we can select the bones we want in the hierarchy to only view the keyframes we need second to last we have the diamond and plus button this is the add keyframe button this button is intended for use when we are in preview mode and we might have made some changes but record wasn't active it will insert a new key at the current frame of the timeline or it will override the current value of a keyframe that is in the same frame and finally we have the add event option which is one of the main focuses of this video pressing on this button will add a new event marker at the current frame of the timeline for this animation animation events are indicated by the small white line added to the timeline if pressed more than once in the same frame the events will stack so what exactly do animation events do an animation event is used to call functions at the specific frame in an animation as long as the events function can be found in a script that is attached to the same game object that the animator and animation are attached to and the function is also defined as a public function in the script the event can do practically anything that the programmer wants depending on our active selection we'll see a different inspector window when pressing on the event in the timeline if we are actively selecting the cube in the scene hierarchy and press on the event in the animation window we'll just see a drop down menu that will list any public functions accessible through any of the cube scripts however if we select the animation in the project window and then press on the event the inspector will allow us to enter a function name and a custom parameter that we want to be passed to the function the difference here is that rather than already knowing what functions are accessible when the cube is selected unity is basically assuming that the game object the animation will be attached to will have access to a public function of the same name and if the function cannot be found we'll see an error thrown when passing the event frame in the animation let's take a look at some examples of animation events again events can call any public function so the possibilities are near limitless events can be used to instantiate a projectile bullet at the correct time in an animation to trigger a sound effect or voice line to trigger a new animation state to reload a weapon's ammunition value and so on okay cool the last setting we haven't covered is at the bottom of the menu tab we'll see the current selection is dope sheet with the alternative curves to its right selecting curves we'll notice our timeline gets a fresh new look as a line graph and we'll see colored lines now connecting the keyframes these colored lines are animation curves and they represent the path that the associated property will take from one keyframe to another additionally the timeline now has a y axis and keyframes are now presented at their properties values to explain this let's take a deeper look at our cube in the menu we can open the transform property to see the position x y and z next to each value in the menu is the color that the value is represented by in the timeline based on the keyframes we had set earlier in this video which were changes in the y-axis we can see the interpolation path and transform y-values our cube will take during the animation as we step through the timeline frame by frame we'll see that the value accurately follows the curve why would we want to use the curves menu we understand that each curve represents the interpolated path each value will take between each keyframe well now we have an easy way to adjust this path pressing play and moving the middle keyframe up and down we'll see this completely changes the animation again this is because the keyframe in the curves menu actually represents the value of the property but we'll also notice two smaller diamonds connected to the keyframe these are known as tangents and they control the shape of the curve between each key as we drag a tangent up and down we can see how dramatically the curve is modified completely changing the path of the cube and therefore the animation if we right click on a keyframe we have the option to not only delete and edit the keyframe but we also have access to different styles of tangent types modifying our tangent type can drastically impact our animation so let's take a second to learn each by default unity sets the tangent type to clamped auto this setting will adjust the two tangents so that no matter where we move the keyframe the tangent will adjust to create a smooth curve through both sides of the keyframe auto is an older version of clamped auto and exits solely for backwards compatibility on older versions of unity the main difference is that the tangents aren't updated after they are initially set and like we can see here auto also does not avoid overshooting the value of its surrounding keyframes and this can result in unwanted interpolation values by adjusting either the right or the left tangent the tangent type is automatically set to free smooth which guarantees that the curves coming out of both sides will be collinear collinear basically means that they fall on the same line or headed in the same direction this again ensures smoothness between keyframes flat will literally flatten the tangents to a straight horizontal line and the last option broken allows us to adjust the left and the right tangents independently and we can also set the type of the curve that we want for each side free linear constant or weighted free is a standard curve that we've been working with thus far linear being a straight line between the connecting keyframes that have linear set and constant which does not change the properties value until it reaches the next keyframe weighted is the most customizable of the group allowing us to bend the curve into the shape that we want playing with the tangents is a great way to learn just how powerful animation curves can be they can drastically change our animations and when properly combined with animation events they can have some awesome results now let's take a look at an imported animation from xml.com and see how we can use our new animation skills to enhance it i went ahead and added the necessary animator and animator controller so that the character will play the animation and i added a small pistol weapon as a child of the right hand bone in play mode we can clearly see a lack of bullets firing from the weapon as the animation plays let's use our new knowledge of events to have a bullet fire from the weapon at the proper frame in the animation with our character selected in our animation window we can see that the currently selected animation is titled read only to fix this all we need to do is duplicate the animation that was attached to the fbx file and then select the copy in our animator now when we look at the animation again we can see that we can modify it scraping through the animation to find the frame just before the recoil of the shot we'll add a new event that will fire a bullet i've gone ahead and created a small laser beam prefab an empty game object called bullet spawner that is placed directly in front of the pistol's barrel and set it as a child of the weapon so that it follows the pistol through the animation and lastly a script that has been attached to the character game object the script has a public function called fire bullet that will instantiate a new bullet prefab and add force to the rigidbody to make it actually travel forward entering play mode and voila the bullet is instantiated and fires at the right point in the animation awesome but again the possibilities are near limitless with animation events because it's just triggering a function how else can we modify the animation with our new skills playing the animation we'll see that the weapon is currently clipping through the character's head at certain keyframes of the animation fortunately we know that we can modify the keyframes and the property values entering the curves menu we will see a web of animation curves we can isolate the right arm by selecting it from the menu and here we'll see that by just modifying a few keyframes we can fairly easily solve the clipping issue awesome i think we can agree that the animation is also lacking a little oomph or impact selecting the spine in the hierarchy and activating filter by selection we can see the spine currently has a few keyframes we know that we can modify the keyframes if the record button is on so at the same keyframe that the event is currently on we'll set a new keyframe using tangents we can modify the rotation properties curve between these frames which will make our character's rotation start to lean back and then forward again to represent recoil it's looking good but the timing of the animation still seems a little slow let's modify a selection of our keyframes after the event by scaling them down by decreasing the scale of these keyframes the select portion of the animation will increase in speed we can really emphasize the power of the shot take a look at a comparison between our original animation and the current one we can clearly see that after just a couple of minutes of modification we've really customized the animation to both fit our character and to exaggerate the motion leaving a sense of visual impact as we can see with our new understanding of animations we can not only make new animations but we can modify old ones to completely change the feel of the animation if you learned something new please consider liking this video and subscribing to the channel it really helps the channel reach amazing developers like yourself in future videos of this series we'll take a look at the animation ranking package and inverse kinematics this community is growing and we'd love for you to be a part of it a link for the channel discord is in the description and if you want updates on the next video feel free to follow me on twitter but that's all for today thank you so much for watching and i'll see you in the next video
Info
Channel: iHeartGameDev
Views: 21,023
Rating: undefined out of 5
Keywords: Animations in Unity3D, how to animate characters in Unity3D, how to animate in unity3D, iHeartGameDev, unity3d animations, unity animation tutorial, animation events, unity animation 2d, animations unity, animate characters unity, unity3d animation, animations explained in Unity, unity3d animations explained, how to do animations in unity3d, tangents, unity3d, how to use animation events unity, how to use animation curves in unity, how to animate characters in unity3d, unity 2021
Id: URjXL0QXwm4
Channel Id: undefined
Length: 15min 18sec (918 seconds)
Published: Sun Nov 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.