From the course: Unity 2023 Essential Training

Animation basics and editors in Unity - Unity Tutorial

From the course: Unity 2023 Essential Training

Animation basics and editors in Unity

- [Instructor] There are many ways to animate in Unity, and we'll explore some of them in this course and start with the simplest way to animate, with the animation window. So the animation window allows you to create animation clips inside of Unity. So later on, we'll explore the timeline, which allows you to create more complex animations. So let's get started with the animation window. So you get to the animation window by clicking window, animation, and animation window, and the shortcut is command six for Mac and option six for Windows. So let's go ahead and add it, and what I'm going to do is drag it inside of the bottom screen here like so, and I'm going to give myself a bit more room to play here. So the first thing you need to do, before you animate anything, you need to select an object in the scene. So what I'm going to do is select this piece of chair, and again, I'm going to make sure that I select the full chair and not just the hoops here, so let's go ahead and do that. And once you have the chair selected, and you can move it to make sure that you have the right thing selected, and you do, you create an animation. So I click create, and what I want to do is create an actual folder for animations, and this is where all of our animation stuff is going to go in. So I'm going to create a folder inside of the assets right here, new folder, and I'm going to name this folder animations. Perfect. So this will be chair three animation, so I'm going to name it accordingly, 'cause we're going to need it when we go into the controller or the state machine and have multiple animations within one controller. So this will make sense in the next video. So I'm going to call this chair_3, just to make it... Perfect. So now I have this window here that I have created for chair three. So the way to add an animation is to add a property. So let me explain this. Everything inside of a game object can be animated, so all the elements of an object could be animated, so you create a state or a key of specific elements of an object, then you move it through the timeline and then you create another state for that particular element in the inspector. So let me show you what that means. Let's go ahead and add a property. So when you click on add property, you have the chair itself and you have transform for the whole chair. So you can literally select one of these and go ahead and add an animation. But if you want to animate anything within the chair itself, you can actually go ahead and select the hoop, the legs, or anything else in there. So like I mentioned, anything within a game object can be animated. So I'm going to go ahead and hit the transform on here and use the position, 'cause that's what we're going to animate. Once you have this here, you have these positions that you can animate. You have the initial keyframes and you have the end keyframes. So these are basically, if you play it right now, nothing's going to happen, 'cause we didn't animate anything between all these keyframes, right? But the other thing you need to understand is that this is milliseconds. So right now, we have just one second of animation, so if you want more, you need to add more seconds here. So you can also select frames as opposed to seconds, but I like to play in actual time versus frames. So I'm still going to have seconds selected, and what I want is create keyframes at 300 milliseconds, which is basically five seconds. So I'm going to delete those keyframes and then hit 300, and then create new keyframes at the 300 mark. So right now, we have keyframes at 300, or five seconds, and by the way, if you want to stretch your timeline and make it shorter or seeing it all, what you can do is actually click on this side here and stretch it in a way where you can actually see all your keyframes in one screen. So right now, this is the position at the keyframe here, so what we need to do is change, for those keyframes, where it's going to be at this. So if we select this here, this is where it is at this position on the Z axis and at this position on the X axis. So if you want to change it for the end, we need to select those keyframes and then move it within the position here. So if you want to know exactly where to put it here, you can actually move it first, and you'll know exactly where those keyframes you want it to be, and you can change it so minus 0.4 or 0.5, minus 0.5, and then for the Z axis is minus 0.67. Now, if you play the animation right now, what it's going to do, it's basically going to start from where you had it in the scene and then move it by minus five in the X axis and minus six in the Z axis. But we wanted it to start where it was initially, so what we're going to do now is basically stop the animation and make sure it is in the position where it was initially, like here. So when we actually play the animation, it's going to move it from where we wanted it over time. So if you want to change it somewhere else or if you had to change the animation, you can change those values, but at least we have a five second animation for the chair right now. So you can make changes... Hold on, let's just get the initial state properly. So this is where I want it initially, and this is the initial state, so if you go back to here, you see it's at 0, 0, 0, and when you play it, this is where it's going. So you can change the animation as you see fit. So right now, it's at 0, 0, 0, 0, and then it's going to move by these numbers. So if you don't like what the numbers are, you can change as you wish. So you can also take a look, okay, so I want it by this position, by minus four here, and so on so forth. You can change all of this to make the animation that you want. So at the end, let's go at the end. So if you don't like this, you can do, okay, so I don't like this here, I want it more here at the end, and then you can change the values for the original keyframe so when you play it, it's going to be where you want it to be, like that. And voila, you have your very first animation in Unity. So feel free to play around with the options before you move on to the next video.

Contents