Player movement setup for Galaxy Invaders game-pt.1
Objective: Starting off with the Player’s basic movement on the X-axis using Unity’s Transform.Translate class and method. Also, using the hard coded value in the equation and the preferred variable’s name in the equation to move the Player.
To understand the player movements along the axes using Vector math, select the Player object and the Move tool. We start off at zero which is the center of the screen. If we move to the right along the X-axis, we will notice the player’s movements increment by one. If we move to the left, the player’s movements decrement by one. Moving up on the Y-axis will increment by one, and moving down decrements by one.
Inside the PlayerBehavior script, we will go to the Update() function and use the Transform class. Under the Transform class is a method name Translate which moves objects along the axes. Using the transform.Translate() commands, make the Player move along the X-axis to the right using the Vector3 structure. This is represented in Unity as: Vector3(1, 0, 0) which means 1 on the X, 0 on the Y, and 0 on the Z axes.
We see the Player moving along the X-axis 1 meter per frame to the right indefinitely.
Now what if we decided to add speed to the Player movement by multiplying 5 to the right of Vector3.
The Player moves 5 times faster and maybe too fast for the human eye to keep up indefinitely.
To make the Player have more realistic movements, we can use real-time to the equation with the Time class. Inside the Time class is a method called deltaTime which moves in seconds from the last frame to the current one. The equation will look like this: transform.Translate(Vector3.right * 5 * Time.deltaTime) which is the same as (1, 0, 0) * 5 * real time. 5 multiplies with 1 on the X-axis to equal 5, and 5 multiplies with the real time which will make the Player movements more realistic.
Notice that the Player is moving 5 meters per second, but in real time now which allows us to see the movements in a more realistic manner.
Hard coding the 5 into the equation is not good programming practice because every time Update() have to move the Player it will have to keep referring back to the equation and doing the process over and over again which can slow down the performance of the game. Instead, let’s create a new Private variable name Speed to store the value in it which is the value 5 and can be stored in a memory location for quicker access.
When creating a variable, 1st thing to declare is its Access Modifier which will be Private for this variable as we don’t want any other objects changing this value. Next is the Data Type which will declare what type of value will be stored which will be an Int, short for integer in this case. Then create a name for the variable, and since this is Private use the underscore “_” before the name of the variable as this will differentiate the name from the Public variables created which is a good programming practice. Finally, make the variable equal a value which will be 3 for this example, and change the 5 out of the equation to the _speed variable name.
Now the Player is moving at 3 meters per second using the variable’s _speed name.
Another thing we can do is allow Game Designers a chance to change the values of the Speed variable by giving them access to the variable in the Inspector. To do this, use the SerializeField attribute either above the variable or on the same line towards the front of the Access Modifier which is Private.
After saving your script, select the Player object and go to the PlayerBehavior script component. Notice the Speed variable name and the value’s slot now accessible to be used to change the Speed’s value. We began the game at 3 meters per second, but as the Player was moving we changed the value to 10 meters per second. See the difference in having the ability to change this value. Any changes made to the Speed value in the Inspector before playing the game will override the value entered in the PlayerBehavior script itself permanently.