Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Project: Scripting basic animations
See Details
Joshua Kinney


Joshua Kinney
In this final lesson, we're going to learn how to apply animations using script. All right, so go ahead and make sure that you're in the Advanced_Movement scene. And I want to go ahead and create this Lesson_18_Anim_Controller script here using C#. And let's go ahead and open that up in MonoDevelop. So what we need to do is we need to assign animations according to the keys that we're pressing. So to get started with this, let's go ahead and start in our Update function, and let's start to put in some of those if statements. Now, you might be wondering, why aren't we just continuing on with the lesson 17 movement? Why didn't we just add it to this script that we've already created? Well, if we were to do, what would happen is we would have to apply animations to our character controller, and that's not going to work. So we have two different components to our player. We have the character controller, and then we have the geometry of the player, which is that FBX file. So if we expand this out, we have the player, or the prefab player. And on this, the animations lie on it. So we have to apply any scripts with animations to this object. It can't rest on the character controller. So that's why we have the lesson 17 here. And then we're going to apply the lesson 18 to this object. All right, so let's get started. Let's create an if statement. And we're just going to say if Input.GetKey. And we're gonna set this to the d key for right now. So if we press d, which is going to be forward, we want it to go through the animation itself. Now, with the Input Manager, the d key is also associated with the right key on the keyboard. So let's use our logical statements, so we're gonna say or Input.GetKey. And we're gonna set that to the right key. And then we're going to end that with double parentheses to close that off. And then let's tell it what to do in our if statement here. So if this is true, we want animation, and then we're gonna say dot. Now what we want to do is we want to be able to blend in between the different animations. So whenever we're in an idle animation, the character is just resting, we want to be able to press the key, and we don't want it to just snap to the animation and start that way. What we want it to do is we want it to blend in and look natural. So we're going to use what's called Crossfade to help us out with this. Now, if you want to learn more about animation scripts, again, bring up that Scripting Reference and look it up that way. So just type in animation, and you can see all of the different variables and functions we have here. So here is that Crossfade function. So you can see it says, "fades the animations with name animation and over a period of time in seconds, and fades other animations out." All right, so back in our MonoDevelop, we're going to use animation.Crossfade. Now we need to tell it what animation to actually use. So the animation name that we're going to be using here, this is going to be our run forward. So where do we get the animation names from? Well, they're going to be nested onto our FBX object, which is our player geometry. With this, whenever we imported it in, you'll see that it has animations that are split. And you can see that I have six different animations to use. And I have an idle, a run forward, run backward, strafe right, strafe left, and death. Now we want to use this run forward for this example. So we're going to type it in exactly as we see it here. So let's go into MonoDevelop, and we're going to put this in quotations. So we're gonna say R-U-N and then F-W-D for run forward. And it has to be the exact way that it's typed in the FBX file. Now we need to put a comma here, because now we need to tell it how long we want it to take to actually fade from one animation to the next. So I'm going to type in 0.2f for float value. Let's end that with parentheses and then semicolon. Let's close that if statement. Now let's go ahead and copy this code, so I'm gonna hit Control C and Control V. Now with this one, I'm going to use an else-if statement. So let's type in else if. And we're just gonna change the key that's being used. So we're gonna say "a". And then we're going to also type in "left." Let's type in run backward, so that was uppercase BKWD. Same amount of time. Now what I'm going to do is I'm going to take this, Control C, and paste that in. Let's change the key for it, so we're gonna use w, and up. And then this one, we're going to strafe left, so "StrafeL." And let's do that one more time. So this time, we're going to use the s key, and then the down arrow. And we're going to type in strafe right, and the same amount of time to fade. Now, the final one that we have that we want to put in here is going to be our else statement. So we want it to be able to go back to the idle whenever we're not pressing anything. So on our else statement, we're just going to type in-- let's format that correctly. We're just going to type in animation.Crossfade. And then we're going to set it to that idle animation. And then we want it to fade in 0.2 seconds. So there we go. Let's end that else with our curly brackets. And we should be ready to go here. So looking at all of our different if statements, we've got all of these paired together. And we're looking good here. Let's go ahead and save this file. Let's go into Unity. We'll check for any errors. It doesn't look like anything has come up. Now, this script, the animation controller must live on a game object that has an animation component. So it has to live on this PFB_Player component. Let's go ahead and drag that onto there. There we go. And let's hit Play. So here we can see that our player is breathing. It's in its idle animation. And let's hit d. And it begins to animate. And a little bit of popping there, so it looks like maybe the key frames are set wrong here. It looks like we're off one key frame. We can run backward. And then we could also jump. Now you'll notice that whenever it's jumping, it's still running in place. And we would have to create a jump animation for that, but we don't have one for this character. But you can see the basis on how this works here. All right, so in this course we've discussed all of the foundational knowledge you need to begin scripting in C#. We've talked about variables and functions, if statements, and we've even talked about arrays, which are really fun. And then we applied this knowledge to create a couple of projects. So we created a basic move script using that box, and then we created a more advanced move script using this player character here. And I've had a lot of fun in this course. I really enjoy scripting. I hope you enjoyed this one, too. And I'll see you guys next time.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.