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: Basic move script
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to take what we've learned in the past few lessons and we're going to apply that to a game project. All right, so let's go ahead and create a new script. And I'm going to call this one Lesson_11_Movement, just so I know which one I'm working with here. So now that we have this script, let's go ahead and double-click on that and open it up in MonoDevelop. And what we want to do is we want to create a very simple move script that will allow us to move an object in our scene. Now, whenever you are creating gameplay functionality, you don't need finalized assets at this point. You can use simple geometric primitives inside of Unity to get started with your functionality of your code. So I'm going to go ahead and format my script the way I like it. And we're going to go ahead and create our code in our Update function. Now, again, our Update function is going to update every single frame as long as we're running our game. So to get started, how do we move an object? Well, if you have no idea where to start and you want to start out just from scratch, a great place to start learning code is to go to Help, and then bring up your scripting reference. Now, we need to go to the Unity window here and go to Help, Scripting Reference. Now, once we click on that, that's going to bring up our Scripting Reference here, and we could start to look at some of the different code that is available to us. So let's say that I want to move. Let's just type in "move" here and see what we have. Well, we have Tool Move, we have NavMeshAgent.Move, and things like that. But this really isn't looking like it's going to give us anything that we need. So let's try a different word for "move." Let's actually try "translate." Here we can see we have transform.translate. And it says, "moves the transform by x along the x-axis, y along the y-axis, and z along the z-axis." Let's go ahead and click on that and see a couple of examples on how this actually works. Now, we can use a translate function, or we could even use translate by using transform.translate and then giving it its parameters. So let's go ahead and do that here. Let's bring up MonoDevelop. And inside of our Update function, we're going to type in "transform." And that's lowercase t on transform. And then we're going to "Translate," which is uppercase T. And then let's go ahead and put in our parameters. As you can see here, we're getting a little bit of a hint. So it's saying the translate has to use a Vector 3 translational. So let's go ahead and type in a parentheses there. And let's tell it how to move here. Now, I want it to move by input with keys. And what I'm going to do is use input. We've seen this before already. This time, we don't necessarily want to use GetKeyUp or GetKeyDown or anything like that. We could use it, but I want to use the input settings, the Input Manager inside of Unity. So to see that, let's open up Unity. Let's go to File, or actually I believe it's Edit. Here we go. Project settings, and then Input. By bringing this up, you'll see that we have axes. And in our axes, we have several different types of axes that we can use. We have horizontal. So if we go ahead and expand that out, you'll see that I can use the negative and positive buttons here of left and right on the keyboard. And then I also have an alternate negative and positive button, A and D. So I could actually use this horizontal axis to move left and right. So let's go ahead and use that inside of Unity in our MonoDevelop. So Input, and we're going to say GetAxis. And then we're going to open parentheses, and then open quotation and give it the name of that axis, which was "Horizontal." Now, once we've done that, let's close that parentheses there. And then let's do comma. And now we need to tell it how to move in the y. Now, y is going to be straight up and down in Unity. Now, I don't necessarily want any movement in the y, so I'm just going to type in 0. And then we want to be able to move along in the z-direction, so I want to move forward and backward. So by taking a look at my Input Manager again, I can actually get the axis on this. And this time, it's going to be called "Vertical." So we'll type that in, close quotations and then close parentheses. And we want to make sure that we have all of these parentheses paired up. I do have one out here at the very beginning, so I need an extra one out at the end. And then we're gonna do a semicolon here. Now this simple line of code here is going to allow us to move our object inside of 3D space. So let's go ahead and save this. Let's go into Unity. And then what we're going to do is we're going to go ahead and create a plane. So I'm going to go to Game Object, Create Other. And let's create a simple plane. And I'm going to go ahead and scale this up a little bit. Let's go to 10 on our x and then 10 on our z. And let's try that one more time, 10. There we go, just to make that a little larger. And then I'm going to go to Game Object, Create Other. And let's create a simple box. Let's go ahead and zero out its translations here, so 0, 0, 0. And then let's take our y up. Let's bring that above our plane. And that's going to be around 1.5, I believe, so to sit right on the ground. Well, it looks like 1.6 is going to be the value we need there. All right, so now that we have our box, we have our plane, let's go ahead and create a light. So I'm gonna right-click-- well, let's go to Create, and then let's do Point Light. Or do Directional Light. That one's a little bit easier to control. So now what I can do is I can see my object in my scene. And if you need to, you could drop the intensity down a little bit, just to make that a little bit easier. And let's select our main camera and make sure that we can see our box. I'm gonna go ahead and bring this up here, just so I can see what's going on. Now let's apply our script to our cube. So Lesson_11_Movement, apply that to the cube. And you can see that is now applied as a component. Let's hit Play. And now if we hit W, A, S, and D, we can see that we can move our box. Now, there's a couple of issues with this. We have a box that is moving very, very quickly, and we can barely see what's even going on. So what we're going to do in our next lesson is we're going to add some variables to this movement script to slow it down, and we're actually going to slow it down to the speed of actual time instead of the speed of the computer itself. So we'll do that 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.