Now we're using some Vectors! If you're not familiar with Vectors then there is some learning you
may need to do online, they can be both complicated and simple but in the end they are your best friend
when it comes to controlling how objects move in a 3D world. Notice I am using the word 'new' in
some of those lines of code, this is purely C# syntax, it needs to do this when assigning new memory
for new variables I believe. The first line of code is finding out what keys the player is pressing,
remember in (edit - project settings – input) one of the input fields was called Horizontal?
Input.GetAxis("Horizontal") is giving us a variable between -1 and 1 telling us which key is being
pressed, pressing A will give us -1 and pressing D will give us 1.
Next we are finding out the rotation of the character, inputRotation is a Vector3, which calculates the
Vector from the center of the screen to the mouse position. I wont go into the mathematics of this but
that is what is happening in those few lines of code.
Now we are processing the movement:
void ProcessMovement()
{
rigidbody.AddForce (inputMovement.normalized * moveSpeed *
Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y +
180,0);
transform.position = new Vector3(transform.position.x,
0,transform.position.z);
}
Because earlier we added a rigidbody component to our player character, we can now call the
AddForce function to move it. Rigidbody is a pointer which automatically refers to the rigidbody
component attached to the gameObject using the script. If we didn't have a rigidbody attached to the
gameObject and were referring to it in a script, we would get an error. Remember our Vector which
held the information which keys the player was pressing? We are now putting those keys onto the X
and Z axis of the real player and moving it accordingly. When we use the .normalize function of a
Vector3. We are restricting the Vector to a size of 1. If we didn't do this there might be times in which
the player would move at different speeds because the Vector might be at different sizes. We are then
multiplying the Vector by the movement speed of the player and by Time.deltaTime. I don't know if
Time.deltaTime makes a difference here but as I'm used to in my 3D Game Studio days, it is used to
make the movement smooth and consistent depending on the framerate, if this line wasn't here and the
framerate change the speed the player moved might change also.
Quaternions! They are lovely! Transform.rotation is a Quaternion type of variable, and we cannot
simply tell it to accept a Vector type, so using the LookRotation function we are converting the Vector
into a Quaternion. And setting the rotation of the transform to the rotation of the inputRotation Vector.
Because this is a 2D game, we don't want the object rotating on any other axis asdie from the Y axis,
because we are applying a force on a cylinder collider the object will often rotate along the X and Z
axis as it pleases. In the 3
rd
instruction of code we are simply reseting the rotation along the X and Z
axis to 0, and maintaining the original rotation along the Y axis. We rotate the Y axis eular angle by
another 180 degrees because in the sprite's material we set the tiling to -0.25, this means we must rotate
it around to make sure our object is facing the correct direction.
Page 9 of 44