Simple Player Movement in Unity
Objective: Set up a Player token to move freely along a two dimensional plane.
Movement is pretty universal across games. While there are exceptions, in most games, your player needs a way to move. Here, we’ll be going over how do that on along the X and Y-axis. I’ll also cover how to set player boundaries and how to have the player jump to the other side of the scene when they reach the boundary. I’ll be operating under the assumption that you are already familiar with navigating Unity, creating objects and scripts, and that use have a basic understanding of C#.
To start, you’ll need to make a game object to represent the player. In this case I used a sphere. Then you should create a new script called “Player” to store the code used to manipulate the player object. Make sure to also assign the script to the player object.
Now we’ll want to set the starting position for the player. To that, you’ll want to use “transform.position” and create a new positional marker, or Vector3, in the “Start” method to set the position. In this case, I’m using (0, 0 ,0).
From here on, I’ll be creating a new method called “Calculate Movement” that I’ll call within the “Update method.” I’ll be storing all the code to move the player within this method.
To start with movement, you’ll want to create a variable to store the speed of the player. This will let you reuse that variable as needed, and make change it a lot easier. I made this a float so we can store decimals. I made this a private variable, since it’s unlikely this will need to be accessed outside the Player class. “[SerializeField]” will let us override this variable within the Unity editor, without making the variable public. Set the speed to how many units you’d like the player to move per second.
Next, you’ll want to make sure that you have the player input wanted for you’re directional movement. In the Unity editor go to “Edit” then click on “Project settings…” Within the window that opened, go to “Input Manager” and open the dropdown for “Axes”, then “Horizontal.” Here, set the “Negative Button” (left), and the “Positive Button” (right) to what you want them to be. You can also set the Alt buttons as well. As a note, if you want the left/right arrow keys, you’ll need to type the word “left” or “right.” After you do that, do the same for “Vertical” (negative is down, positive is up).
Now, in the script, we’ll want to tell the script to read the button inputs. For that we’ll use “Input.GetAxis().” Within the parenthesis, you’ll pass a string of the Axes as an argument (“Horizontal”/“Vertical”). We’ll be saving these to floats (horizontalInput/verticalInput) so we can call them easily later.
With those variable set, we need to tell the program to do something with those inputs. To start, we’re going to make a new Vector3 that will store both directional inputs. I’ll be naming my Vector3 “direction.” We’ll save “horizontalInput” and “verticalInput” to the X and Y-axis respectively. The Z axis will be “0” since we’re working in two dimensions.
Now we’ll use that vector3 to actually move the player. For that we’ll use “transform.Translate.” We’ll set this to move by our new Vector3 multiplied by “_speed” multiplied by “Time.deltaTime.” “Time.deltaTime” will cause the player to move “_speed” number of units per second, instead of “_speed” number of units per frame, which would be obscenely fast, though that could be fun.
With that, you can move your player. To make sure it works, I would suggest testing it within the Unity editor. If something was typed incorrectly, Unity should give you an error message pointing to the incorrect line.
Now I’m going to show you how to set up boundaries for the player. We’ll also “wrap” the X-axis boundaries, so if the player moves beyond those boundaries, they will move to the other side of the playable area.
To set these boundaries we’ll use if/else if statements. First we’ll set the Y-axis boundaries. We’re going to set the top boundary to “0” and the bottom to the bottom of the camera view. For the top boundary, we’ll set the if statement to check if the “Y” position is greater than or equal to “0.” If it is, we’ll set “transform.position” to a new Vector3 with the “Y” position being “0.” The “X” position will be set to “transform.postion.x” so the players horizontal position isn’t affected by this boundary.
We’ll now do the same to check the bottom boundary, but first we’ll need to find the “Y” coordinate for that boundary. To do that, simply select the “Player” in the Hierarchy view, hit “play” so you can move your player object, and move the player to the bottom of the camera’s viewable area. In my case that position was about -3.7.
Now that we have that position, we’ll follow the same steps to set the bottom boundary. The only difference will be that we’ll check if the position is less than or equal to the desired boundary, and that this will be an “else if” statement, instead of and “if” statement.
You’ve now set the boundaries for vertical movement. I’d once again recommend testing to make sure everything was done correctly.
We’ll now set the horizontal boundary so that the player will “wrap” around to the other side of the screen when they reach it’s limits. This will look very similar to how we set the bottom boundary. The first step is to find where that boundary is, except instead of stopping at the camera limit, we’ll go just beyond it. for me that boundary was about 16.6.
So in our “if” statement this time, we’ll check if the player position is greater than that boundary. If it is, we’ll set the player’s “X” position to the opposite boundary, or the negative of that position we just found. Your “else if” statement will be the opposite: if the players position is less than the negative boundary, the players position equals the positive boundary.
With that you’re all done. Once the player moves beyond those boundaries, they will appear at the other side of the play space. Test it to make sure everything works correct the you’re all set.
You’re all done now. You have successfully limited the player objects movement, so it moves how you want it to. You can change boundaries and speed as you wish to fine tune the experience.