Simple Player Movement

Austin Hall
4 min readAug 26, 2022

--

Learning object movement is essential for any interactive gaming software. To understand this concept, we first need to understand what is actually happening to the object. In a 2D game the player object will move along the X and Y axis. As I move the object in the scene window the player object in the game window moves. Also, notice the position in the inspector window on the right. See how it changes as the object moves.

The goal here is to use code to allow the W,A,S,D keys and the arrow keys to manipulate the object. first we need the starting position of our object. for that we use the following code snippet. This takes the current position and moves the object to the starting position of (0, 0, 0) on the X,Y,Z axis, respectively.

Now that we have the starting position, we can use the Translate function to move the object. Before we do that, we need to create some variables and assign some values to them. We will use ‘_speed’ ‘horizontalInput’ , ‘verticalInput’ , and ‘move’ as our variable names. They will all be of float type.

First we define a private variable as speed and set it equal to 10.0. The f in the picture stands for float and is required for the code to run properly. The underscore is used to identify the variable as private. The [SerializeField] is necessary if we want to see the variable in the inspector tab in Unity.

Speed variable is accessible in the editor

Next we’ll create a method called ‘CalculateMovement’. We need to create some instance variables to use within this method. For that I chose ‘horizontalInput’ , ‘verticalInput’ all of float type, and ‘move’ which will be a Vector3.

For the horizontal and vertical input variables, I use the GetAxis method to pull the horizontal and vertical position from the editor as the object moves (translates). Assigning the vertical and horizontal positions to there respective places in the Vector3 move variable, allows us to assign vertical and horizontal movement simultaneously. Unity has default key mapping built in, so we already know that we can use the W,A,S,D and arrow keys for movement.

transform.translate() is the method we use to move the object. Time.deltaTime does the calculations and changes our speed from frame dependent to real time seconds, minutes and hours. By multiplying the move variable by _speed and the change in time, we get movement in a given direction at a constant rate over time. We accomplish the constant rate of speed over time by calling the CalculateMovement() method inside of the Update() method. Update() is called approximately once per frame and runs at a default speed of 60 frames per second.

Vertical and horizontal movement limits

Above is an example of how we can set limits to our movement. We use if statements and else if statements to compare the position of the object to the limits of our playable space.

This will prevent the object from leaving the playable area, and in this case, in particular, allows the object to wrap around the x-axis. When it reaches one horizontal limit the object’s position is reset to the other side.

--

--

No responses yet