Unity Coding — Player Movement
At this point you should have Unity all downloaded and your editor set up in a format that makes you comfortable. Now let’s dive in and make something happen. Together we are going to make a player move.
At this point we are nowhere near making a fully animated character, so we are going to go back to video game roots and make our first character a simple cube. In the Hierarchy, you should see a ‘+’ right under the tab. You’ll see a long list of possible objects to make. In due time you’ll get to more of them, but for now you are going to click on ‘3D Object’ and make a cube. There it is, your first player avatar.
Time for another big step. You’re actually going to write some code. Before making a script, where you write your code, I would recommend making a ‘Scripts’ folder within the ‘Assets’ folder in the Project window. Should help keep things nice and organized. Once you’ve done that right-click that folder create a C# script. Name it something that makes sense to you and double-click to begin.
You should see a blank script that has two functions in it. Those being Start() and Update(). Start() happens at the beginning of a Scene running, or when the object holding that script is created. Update() happens once every frame or about 60 times per second. This is where we are going to put most of our code for this task.
If you highlight your cube or player in the Hierarchy, you’ll see it show up in the Inspector. At the very top you’ll see the Transform, which contains the position. If we are moving this player, the position is exactly what we want to change. Knowing that, we are going to go into the Update() function and type transform.Translate(). Translate is the function that will help us move this cube.
Fill the rest of that function like so, transform.Translate(Vector3.right * 3 * Time.deltaTime);. This may look like a lot, but be sure to get those parenthesis correct and be sure to end the line with a semicolon and we’ll break it down. A Vector3 represents the 3 parts of a position (x, y, and z). Vector3.right represents horizontal movement. We’ll get to vertical movement in a bit. 3 is a number representing the speed of the movement that is then regulated by Time.deltaTime. Remember we said that Update() happened around 60 times a second. Time.deltaTime changes this for the specific line to better move in real time.
If you attach this script to the cube by clicking and dragging it on, then run Unity, the cube will immediately go to the right. Cool, it moved! We want to make it so that the player actually has control over this movement. This is where input comes in. Unity has built in keycodes for certain inputs. Type in the following at the beginning of Update():
float horizontal = Input.GetAxis(“Horizontal”);
float vertical = Input.GetAxis(“Vertical”);
These are local variables to get player input for the horizontal and vertical axes. As with the standard of games on a keyboard, these are the arrow keys and WASD keys. Don’t worry about the ‘float’ variables for now. That subject will be covered another time. Now we have to adjust our Translate() to take in the input. Use the following what you just typed:
transform.Translate(new Vector3(horizontal, vertical, 0) * 3 * Time.deltaTime);
Now run it and see for yourself. Voila! You, the player, are now controlling the cube. This is it. This is your first game. It is not much, but you made something that allows a player to move a piece. All this with your own code. Congratulations. Big things are ahead for you.