Player Controls Added to My Space Shooter 2D Game

Player controls and movement is one of the coolest things about video games because YOU are controlling something in the

The player in the game right now is our blue cubed hero. I have learned in Game Development, the games are prototyped with simple objects before adding all the pretty art and sounds. The code…

The first thing we wanted to do is give the player a starting position when game starts up, so we need to grab the current position of the cube and give it a new Vector3 (x, y, z) of 0, 0, 0.

This is where the fun begins. We created a new method, which is going to get the Input on the Horizontal and Vertical axis. In Unity, those Horizontal axis are mapped to the Left/Right arrow keys or A/D keys and the Vertical axis are mapped to the Up/Down arrow keys or W/S key. When using these inputs, they will return a value between -1 to 1. On the horizontal axis -1 will be moving left and 1 will be move right. On the vertical axis 1 will be moving up and -1 will be moving down. 0 is return if there is no input. These values are being stored in local float variables.

Time to create velocity! Velocity is direction and speed, so creating a new Vector 3 variable (velocity) that uses the horizontal and vertical input (direction) and multiples it by the speed (more about this in a future article). The last thing we need to do is tell Unity to apply our velocity to transform of the cube and translate it by the velocity * time and boom! We have movement! So cool!

Lastly, they needs to be updated constantly so we will call the method in Update.