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: Refining the movement script
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to refine our movement script. All right, so let's go ahead and create a new script here, just to make iterative. And we just want to see the progress of this script as we go along. So we're gonna call this one Lesson_12_Movement. And let's go ahead and take the script from lesson 11 and paste it in lesson 12 here. So lesson 11, gonna go ahead and hit Control C, select everything in here, Control V. And then make sure that you change the name of the class, and then save that. And there we go. All right, so taking a look at this script, we have an issue here. If we were to go in and play our game, and we started pressing all the buttons at the same time, we wouldn't be able to get a good diagonal direction. So what I want to do is I want to split the x- and the z-directions onto their own variables. And then I want to be able to translate those by actual time instead of the computer's time. I want to be able to slow it down. So the first thing that we're going to do is we're going to go ahead and comment out this code. Let's come up above it. Let's give ourselves just a little bit of room. And let's start talking about what variables we need. Well, the first variable that I need is going to be an x-variable. And then the second is going to be a z-variable. And what's going to be placed on those is going to be the horizontal axis, and then the z is going to have our vertical axis. And we just want to split that apart. So I'm going to go to type in a variable in here. So we're going to use the variable Vector3. And we're going to go ahead and we're going to make that the variable x. And let's set that to input.GetAxis. And we're gonna say "Horizontal." So this is looking familiar here. And then we're going to go ahead and set up the way that this should actually move. So Vector3 works a little bit differently. I can't just say "Horizontal" and tell it to move that way. It doesn't work quite that way. With Vector3, it needs to know which vector to actually move along. So to do this, I'm going to use the Vector3 shorthand for getting the axis that I need. So to look this up, let's go ahead and bring up our scripting reference. And I'm just going to type in vector3. So let's go ahead and open that up. And let's take a look at the different variables, constructors, functions, and classes that we can use. So here I have this class variable. And you'll see that I have "zero," "one," "forward," "up," and "right." And this is the shorthand writing for Vector3, and then giving it a direction of x, setting it to 1, and then y and z are at 0. And then I have up, which is going to be in the y-direction, as you can see represented here. And then I have forward, which is in the z-direction. I have 1, which is going to give us an x, y, and z in all directions. And then I have 0, which will zero out all of the transforms here. OK, so let's go back to MonoDevelop and let's type in multiplied, Shift-8, transform.right. Now at this point, what I've done is I have just re-created this portion of the script. But the issue that I'm having here is that this script right here is moving with the computer speed. So if your computer is extremely fast, you're going to notice that your movement is really fast. Now, even on some of the slowest computers, your box is still going to move really, really fast. So what we need to do is we need to slow down the movement script here to work with actual time on the computer. So we're going to use some scripts, and we're going to pull from the system.collections namespace to do this. So we're going to multiply this movement. And we're going to multiply this by Time.deltaTime. And then what we're going to do is we're going to multiply that by a speed variable that we want to set. So we want to be able to speed it up or slow it down however we see fit. So I'm going to multiply that by speed. Now we haven't actually created this variable yet. So what do we need to do? We need to come up here to the top of our class, and let's create our first variable. So we're going to call this public. And we're going to give this a float value. So I'm gonna say float. And then we're gonna call it speed. And we're going to set that equal to 3.0. And with float values, you always need to include a lowercase f at the end of those values. Let's hit a semicolon there to end that variable statement. All right, so now we could go ahead and test this if we wanted to. So I'm going to go ahead and hit Save. Let's go into Unity. Let's make sure that there are no errors. And it's saying variable x is assigned, but its value is never used. Well, what we've done is we have defined this equation, but we're not actually using it to translate anything. So you can see here, we've missed our transform.translate. So below this, I'm going to give it two spaces here, because I'm going to need room for my x-variable. Let's go ahead and say transform.translate. And we're going to say x. Let's go ahead and save this. Let's go into Unity. That warning should go way. Let's go to our cube, and let's remove lesson 11 and apply lesson 12. Now you can see here that we have our exposed variable speed, and we can adjust that however we see fit. But I'm going to go ahead and hit Play on this. And let's use W, A, D. Or I'm sorry, just A and D, because we've only used this for our movement, for left and right. And you'll see here that our box moves a lot slower. Now if we wanted to, we could double that speed by typing in 6, and then you'll see that our box moves a little bit faster. We could increase it even more, and it moves even faster. All right, so now all we need to do is we need to get our z-direction involved here. So let's create our z-variable. So again, we're going to use Vector3. We're going to set our variable name to z, and we're going to set that equal to input.GetAxis. And what axis are we going to use for the z-direction? Well, we know that we want to use the vertical axis. And what we need to do is we need to tell it how to move. So we're going to multiply that by transform.-- and what was the shorthand that we were going to use? Forward. Now, .up is in the y-direction. And we know that up is as if the box was going to be jumping. So let's multiply that by Time.deltaTime. And then we're going to multiply that to work with our speed variable. So now what we need to do is we need to add the z-variable to our transform.translate. And this is done simply by adding the z-direction, so x plus z. And this will allow us to move in both directions at the same speed. So now let's save that. Let's go into Unity. We can play this now. And now we can move forward and backward. And then we could also move in diagonal directions. And you'll see how smoothly that works. Let's go ahead and adjust our speed to 10. And you can see how this works very easily. All right, so now that we have created a more refined move script for our box, let's get into creating the jump functionality, which we're going to finish up our movement script here in the next lesson.
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.