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: Creating the advanced move script
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to create a movement script based on our character controller. All right, so inside of Unity, what I want you to do is go ahead and import a new package, and we're going to Import, Custom Package. Now, inside of your Project Files folder, you're going to see introduction to C# in Unity, and then you'll see this Unity Scripting level. Go ahead and open that, and it's going to bring up a dialog box that has all of the different objects that are going to be in this level, and you just want to simply hit Import. Now, you'll wait for all of those files to import in, and then you should see this when you're finished. All right, so to get started with our script, let's go ahead and open up our Scripts folder. And inside of my character controller movement, I've created a lesson 16 movement script. Go ahead and open that up inside of MonoDevelop, and I've already formatted this the way that I like. And now, what I want to do is I want to get into creating our movement script based on our character controller. So the first thing that I want to do is I want to go into my scripting reference and look up character controller. Go ahead and click on that link to character controller. You can see all of the different variables, functions, and inherited variables and functions that we can use on our character controller. Now, there are going to be a couple that we're going to see in our script. We're going to use is grounded. And then we'll also use our move functions here. All right, so let's get into getting this started. So let's move into MonoDevelop, and I'm going to go up into my class. I'm going to create my first variable. So the first variable that I want to create is going to be my actual speed variable. So I want to be able to move at a certain rate, and I want to be able to change that if needed. So let's say that I'm holding down D, and I just want to move at a certain pace, but then, if I want to sprint, I can hold down Shift and that key combination, and it will-- we can write a function to actually change that speed. So let's go ahead and put that in. So we're going to type in public, and then I'm going to say speed. Actually, let's put in our data types. I'm going to say float speed, and let's set that equal to, let's say, 6.0 for right now, just to get started. Now, what I want to do is I want to come into my update function, and I want to start creating my move script. So the first thing that I need to do is I need to create a variable for my character controller that's going to be used here. Now, we may have a game where we don't necessarily know what player we're going to use, and we want to be able to use different players, so I'm going to set this up for you. So to do this, let's create our first variable inside of here, so we're not going to use public. We're just going to use the data type variable and that's going to be character controller. And we're going to set this to the variable name controller. Let's set this equal to whatever controller we input inside of Unity. So what I need to do is I need to get the component from the game engine itself. So get component, and the component that we want to get has to be a character controller, so character controller. So it has to be a player object or character with a character controller component attached to it. So what we're doing is we're hard coding that, making sure that that is there. Now, I want to create my input, so I'm going to create a simple if statement, and I want to go ahead and just get this out of the way right now. I want my character to only move if he is grounded. So I'm going to say if controller, which is the variable we just created, if controller dot is grounded. Now, we've seen that in our documentation and I just want to show you how we can use this. Now, let's go ahead and create our actual movement script. So the first thing that we need to do is determine how the player is going to input the move direction. So let's go ahead and create our variable first. So I'm going to say move direction, and I'm going to set this equal to a vector three. So we've already seen how this works, so we're just going to type in vector three. And then we're going to give our input. So input dot get access. And again, we're going to be using that input manager to do this. So the first access we want to get is going to be our x, so our left and right, so we know that that is horizontal. We're going to do a comma, we do not want it to move in the y because we're going to create a jump function for that. And then, we want to move along the z, so we want to be able to strafe left and right in our scroller here. So we're going to say input dot get access, and we're going to do vertical. And then double parentheses on the end of that so, that way, we have all of these paired together, and then end that with a semicolon. So I'm just going to put a simple comment out here, and I'm going to say player input. Now, the next part that we need is going to tell how to actually move. All right, so this is going to physically move our character controller in our 3D space, in our game. So the variable we're going to use is move direction again. And we're going to set this equal to transform, because we already know that that's how we're going to be able to transform the object. And we're going to use a variable called transform direction. So I'm going to go ahead and just show you what that looks like inside of the scripting reference. So on our character controller, we don't necessarily see this. But if I go down into all of my classes and variables here, you'll see that I have this transform-- there's transform, and we should see transform direction under our transform there. So here it says transforms direction from local space to world space. So I want it to move left and right according to my world, and this is going to be very helpful with side scrollers. So the way to use this is we're going to use transform direction, and then we're going to use vector three. So to get started with this, let's go into Unity. On MonoDevelop, we'll say transform dot transform direction, and then we're going to give it the vector three direction that it needs. Now, we've already created this vector three direction, and we're using it with the move direction variable, so that's all we have to input here. So move direction, and then we'll close the parentheses and then end that with a statement. All right, so now, let's go in and let's create how fast we want this to move. So move direction is equal to-- and we're going to create our variable, or we already did, our speed variable. So we're going to actually make this times equals, so we want move direction to equal move direction times speed. So we're just using that shorthand for this, so speed. Here we go. So now, let's create our variable for move direction. We haven't done that just yet. So up here at the top, let's go ahead and create a private variable. And let's go ahead and just use the vector three data type, and we're going to call this move direction. And we're going to set this equal to our vector three. Now, we want to zero this out, because if we have any sort of movement in here right now, it's going to start out moving the character, and we want the character to stand still until we actually press any buttons. So what I could do is say vector three, and then I could type in parentheses here, zero comma zero comma zero, or I could use the shorthand, which is dot zero. OK, so you'll remember that from a few lessons back where we began talking about vector three. We can use the vector three dot forward, which was allowing us to move in the z direction, so that was the parentheses zero comma zero comma 1. And then we also use vector three dot up, which was zero in the x and one in the y and then, zero in the z. So, again, this is just that shorthand. So now that we have created our script, what we need to do is we need to have our speed move at a specific time. We want it to move in the time of the computer-- or at actual time and not the time of the computer, not by frame. All right, so to do this, let's move outside of our if statement. And let's come out here, and let's say controller dot move, and we're going to say the move direction, because it's asking for a vector three motion here. So move direction is a vector three variable, and we're going to multiply that by time dot delta time. All right, so now that we have all this, let's go ahead and save it. Let's go into Unity, check for any errors, and it's saying that we have a literal type of double. So we have a problem with the float here, and it looks like we forgot that f on the end of our variable there, or our value. Go ahead and go to Unity, and now, we've got a couple of others, so let's check these out really quickly. It saying character controller equals get component character controller, so we're having a little bit of an issue with this. So let's take a look at the error again and let's just work this out. So expression denotes a type where a variable value or method group was actually expected. So let's just work through this. So it's saying that there is something wrong with this line of code, and I have a feeling that has to do with this get component. Yes, OK. So what I did was I put these parentheses around get component. Now, in C#, we can't use parentheses like that. What we would use to actually get a component is we use the greater than and less than symbol around the character controller. So that's just a way of assigning that, telling it that it has to have this type of component to actually get, and then, we'll end that with parentheses afterward. Let's go ahead and save this. Let's go into Unity, and our errors have disappeared. Let's go ahead and apply this lesson 16 movement to our player, and we need to apply it to an object or a player that actually has a character controller on it. So you can see here the player has a character controller. It looks like my prefab is just a little off, so let's go ahead and grab the prefab player-- or move tool, and let's just move him forward until he's inside of that character controller. So just up and to the left a little bit, and that's pretty close. All right, so we want to take this script, and let's apply it to the player object because that has the character controller component to it. So once that is applied, let's hit Play, and if we hit W, A, S, and D, we should move here. Now, you'll see that it's not moving so something has gone wrong. So let's go ahead and investigate our script to see what's going on here. All right, so inside of the script, what we'll see is in our update function we have our character controller all set up, so we have no errors here. So, like we said before, if there are no errors in the script, that means that there's something wrong with the logic. So looking at this, I can see that I have my if statement here, and it says my controller, if it is grounded, then it can go ahead and execute this script. Now, what I'm betting is that in Unity, my character is not grounded. OK So if I take a look at the player itself, and if I just rotate around here, you can see that it's just above the ground there, so it's not going to allow us to move. So what we're going to do in our next lesson is we're going to apply gravity to make our player controller always be on the ground, and that will allow us to move. All right, so we'll go ahead and move forward, next.
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.