Important Notice:

The Digital-Tutors site will soon be retired and your access will be turned off - but don’t worry, you can still enjoy all your favorite Digital-Tutors content on Pluralsight (plus thousands of additional courses, skill assessments, paths and more). You can log in to Pluralsight with your Digital-Tutors credentials, as well as update your password at any time on Pluralsight. Please contact us with questions or concerns: support@pluralsight.com.
Scripting the movement
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to create a movement script and hook up our animations. All right. So in the last lesson, we had set up our animations to use the Mecanim system to where we have the transitions from our idle to our walk and then back. And we also created a parameter it is going to control those transitions. And so what we want to do is we want to create a script that's going to take these parameters and hook it into that script, so that way we can control our animation. So to get started, I have scripts folder here. I'm going to right-click and create a new C# script. And I'm going to call this "movement controller," hit Enter, and then we're going to double-click on that to bring up MonoDevelop. Give that just a moment there. And once MonoDevelop has come up here, we want to go ahead and start setting up all of our variables. And so, I'm just going to do a little bit of clean up here. I always like my curly brackets to be inline like that. So if you've done a scripting tutorial with me before, you know how I like to do these, set these up. So let's go ahead and set up our variables. We're going to do our first one. We're going to call this "public." And we're just going to keep this very, very basic. So I'm just going to say public float, and we're saying max speed. We're going to set this equal to 10.0f. Or you could just do 10f. It doesn't really matter. We're going to say, bool facingRight. And what I'm going to do here is I'm just basically checking to see if the player is facing to the right or to the left, just like any other 2D platforming game. So by default we're going to set that to true. And then we also need to hook this into our animator controller, and so I know that I'm going to need this later on. So I'm going to say, animator, and then we're going to give it a name called, anim, which is pretty standard. And then we're going to go down into our Start function. So first thing I need to do is I need to get my animator components, so we're going to say, anim equals GetComponent, and then less than symbol. And we're going to say, animator and than greater than symbol. And then we're going to say, open close parentheses, and then end semicolon. Now, let me go ahead and zoom in here, so we can see this a little bit easier. There we go. So here we have get component animator. And then instead of using the update function, we want to use the fixed update function so that way we are using that fixed update again. If you remember a couple of lessons ago, we had changed the animation to work on the physics update, and so that's what we're going to be doing here. So we're going to say, fixedUpdate. That's going to run every second consistently. And then we're going to say, float move equals. And then we're going to say, Input.GetAccess. And then we're going to tell it what axis we're going to be using. In this case, it's going to be the horizontal axis-- so pressing A or D on the keyboard-- so horizontal. And then we're going to close quotations, close parentheses, end with a semicolon. And then we need to set up are anim, so we're going to say, anim.SetFloat. And this is basically hooking into the Mecanim system. So we're saying SetFloat and then the parameter that we want to adjust here, and it's going to be speed and a comma. And then we're going to tell it what to do. And we're going to say, math f dot Abs, for absolute value, and move. So it's going to bring that in there. So basically what we're doing is we're setting the speed value to our move value. So from here, let's go ahead and bring in our rigid body. Now, we're going to be using a rigid body 2D in this case. And so we're going to go down to rigid body 2D-- and I'm just using the arrow keys to switch between those. And we're going to say, velocity, and we're set this equal to a new vector 2, because we're working in 2D. And we're going to say, move, and then we're going to multiply that by max speed. And actually, we need to do a comma as well. And we need to tell it what direction it's going to go, and so we're going to say, rigid body 2D-- and we want lowercase rigid body on that. So 2D. And it went ahead, and it did it anyway. So rigidBody-- not seeing it here, but let's go ahead and just continue on-- dot. Hm. Why is it doing that move? Times maxSpeed. Let's see-- Rigidbody2D.velocity, new Vector2. Let's see, why are you doing that? Basically, what's happening here is it's not recognizing that, our IntelliSense is not recognizing that there. So I'm just taking a quick look here at what's happening. rigidbody2D.velocity. New Vector2 move, we've got everything set up there. maxSpeed. Let's try it again. Not really sure why it's not bringing that up. Ah, there we go. I'm not sure what happened there. So let's do rigidbody2D .velocity.y. So we're just going to move in that y direction. And then, now we're going to say, basically, if we're moving, if the move value is greater than 0-- basically, if we're moving at all, doesn't matter what direction we're moving, because we use the absolute value. So if our horizontal value is negative-- basically, if we're pressing the A key-- it's going to return it as a positive. Now, we're going to say if move is greater than 0 and not facing right, then we want to basically flip our facing. So I'm going to come underneath here. I'm going to do open close curly brackets. It's just going to be a single line of code, so we don't really have to do curly brackets but I tend to do it anyway. And I'm going to create a function called flip-facing. Now, I haven't created that just yet, but we will here in just a moment. And then I'm going to do an else if statement. So else if move is less than 0 and facing right, we're going to do this. So we're going to flip facing as well. So now that we have that set, let's go ahead and create a new function. We're going to say, void FlipFacing, open close parentheses, open close curly brackets. And then we're going to say, facingRight equals not facingRight. And then we're going to say, vector 3. Now, I know before, we had said we're just doing vector 2's. But in this case, we're going to be scaling our character, and so I just want to flip its scale in the x direction. And so I want to make sure that we can use that. So we're going to say, charScale, for character scale, equals transform.localScale. And then underneath that, we're going to say, charScale.x, so we're telling it which direction it's going to scale. And we're going to multiply that, and multiply is equal to negative 1. And then underneath that, we're going to say, transform.localScale, and that equals to charScale. So basically, we're just setting that. And then that's all that we need here. So just to kind of talk you through this line-by-line. First thing, our variables, we're setting a maxSpeed here. So we're just setting the speed on how fast we want that to move. We're going to make that a public variable, so that way the game designer, whoever's bring that into Unity, can adjust that value based on the animation. bool facingRight, it's set to true, so our character is facing right. That will be set to false if the player presses the A key, so basically it's going to flip that around to where they look the other way. We have our variable for our animator. So it's looking for that controller in which all of our animation clips are tied to. So that has been brought there. And then it's also going to find the speed variable, which is right here-- so anim.setFloat. Speed was a float variable, so we're just going to make that a positive number. So if it's less than 0.01-- basically, if it's 0-- it's not going to move any longer. So move rigidBody.velocity, basically, that is going to push the rigid body. And it's going to base that number off of our move value, which is going to be 0 to 1. And it's going to multiply that by our max speed, which is 10 by default, unless otherwise stated differently. And then it's going to tell what direction we need to move, and that's going to be the y direction. And then we have, if move is greater than 0 and not facing right, we're going to flip it the other direction, so basically it's going to be facing the right. Else, and if we're moving and it's less than 0 and facing right, we're going to flip the other way. So then we have our FlipFacing function, so whatever that is called, it's going to flip the facing. It's just checking to see if it's facing right. If it is, it's not going to be facing right, so it's facing the other way. And then we're taking our scale, and we're telling it to basically flip the scale to negative 1 so that way it appears as though it's facing to the left. So very, very simple line of code here. Let's go ahead and hit File and Save, or you could hit Control-S. And then we're going to back into Unity, and we're going to apply that movement controller to our character. So let's go ahead and select the character, and we're going to simply drag-and-drop the movement onto the character, or you can Add Component here. So here you can see the script has been applied. And if we hit Play, if we hit the D key, it moves to the right. You can see it goes into the walking animation, and then to the left. Now, to me that looks a little fast for the animation that we created. So all I have to do really is come in and set my max speed to something like 2. Lets hit Play again, and then if I move to the right that seems to be a little more the speed of the animation that we created. And you can see that it goes into the idle animation. It flips around. And we're just letting the animator do most of the work here. So we've gone through the process of working with all of the 2D tools and things like that. We've created an animation. And, in my personal opinion, I think this is a more difficult version of the animation, where you have all of the different sprites that are split apart, you pair them all together, you key them all inside of Unity. I think it works really well. I like this system, how it works. I do want to show you one more thing, however, before we end this course. I want to show you how we can use sprite sheets, because it's really, really fantastic. And I think it's really great just to end on. So we're going to go ahead and get started with that in our next lesson.
In this series of training we will discuss the major components of working with the 2D features in Unity. We'll start off by learning how to set up a 2D Unity project. Then we'll learn how to properly import and slice our sprites. After that we'll learn how to manipulate sprites by blocking in a simple platform level and also learn the importance of Sorting Layers.

Then we'll jump into the real fun by assembling, parenting and animating a character. By the end of this tutorial, you will have a better understanding of working with Unity's 2D features.
Introduction and project overview
1

Introduction and project overview

 
00:45
Setting up a 2D Unity project
2

Setting up a 2D Unity project

 
13:05
Manipulating sprites
3

Manipulating sprites

 
14:03
Setting up props
4

Setting up props

 
13:35
Slicing the character
5

Slicing the character

 
13:57
Parenting the character
6

Parenting the character

 
09:25
Creating the colliders
7

Creating the colliders

 
11:10
Creating the idle animation
8

Creating the idle animation

 
17:31
Blocking in the walk cycle
9

Blocking in the walk cycle

 
11:50
Finishing the walk cycle
10

Finishing the walk cycle

 
08:06
Setting up the animations
11

Setting up the animations

 
04:49
Scripting the movement
12

Scripting the movement

 
12:55
Using sprite sheets for animation
13

Using sprite sheets for animation

 
07:48