Player movement setup for Galaxy Invaders game-pt.2

Rhett Haynes
4 min readJun 2, 2022

--

Objective: Moving the Player using the Input Manager GetAxis method. Also, optimizing the code to make it simpler to read while still performing the task at hand.

Player able to move along the horizontal and vertical axes.

As we showed how to move the Player across the screen through code, we now want the end user to control the Player movements across the screen. To do this, we have to give the user control through the use of the keyboard, or mouse, or joysticks. Unity has provided shortcuts to making this happen under the Input Manager. Go to Edit->Project Settings->Input Manager, which provides a drop-down list of Axes. There are 18 axes Unity provides to define the input axes the end user will use, and their associated actions for our project.

Two of the Input Axes we’re going to use are the Horizontal and Vertical axes for the user’s player movement controls. With the Horizontal settings, the user can move the player using the left-key or A-key for the negative button direction. While the user can use the right-key or D-key for the positive button direction. With the Vertical settings, the user can use the down-key or S-key for the negative button direction, and the up-key or W-key for the positive button direction.

Horizontal and Vertical input axes located under the Input Manager.

In the PlayerBehavior script, we want the end user input controls to be checked for every frame. Therefore, we will assign the input controls inside the Update() function by using a local variable name horizontalInput, assigning the value of Input.GetAxis to get the axis we want to use and pass in the “Horizontal” in quotes axis spelled identical to the spelling in the Input Manager Name’s slot. Now place the horizontalInput variable between Vector3.right and _speed to be multiplied between the 2 parameters. Also, we changed the _speed variable from an INT type to a FLOAT type for a smoother experience in the Player’s movement, and the value from 3 to 3.5 keeping it around the same speed for now.

Added the Horizontal Input axis to control the Player’s movement in Translation.

Now when we play the game using either the arrow keys or the WASD keys, notice the player moving around the screen smoothly.

Player moving along the X & Y axes through user input.

Next, we added Vertical movement to the Player by using the same variable type named verticalInput and retrieving the “Vertical” axis. Then, we used the verticalInput in Translation to move Up along the Y-axis Vector multiplied by Speed and Time. When you play the game, the Player will move up and down when pressing the appropriate keys.

However, we can simplify this code on one line by using Translate to move along a new Vector3 passing in horizontalInput on the X-axis, verticalInput on the Y-axis, and zero on the Z-axis. Then multiply the movements by Speed and Time. Comment out the 2 previous lines of Translation and play the game to see if it works.

Optimized the code to move the Player around with one line of code.

Player is moving without any issues.

Player moves with optimized code.

To optimize this further, we can create a new Vector3 variable name direction, and assign its value to equal a new Vector3 handling the horizontalInput, verticalInput, and zero on the 3 axes. This will allow for quicker access from a memory location to retrieve these inputs. Now in Translation, remove the Vector3 assignment and replace it with the direction variable which will represent the Input Axes being called.

New variable Direction created to handle user input.

Game being played and the Player is still able to move up, down, and side-to-side pressing the correct keys without any issues.

Player still able to move under optimized code.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet