Platformer: Player Movement
In this article we will cover how to get started with a Platformer game with Player movement using a character controller.
- Moving left and right using arrow keys
- Adding gravity
- Jumping when the space bar is pressed
We are going to use a capsule primitive for the Player and a Character Controller component. A Character Controller allows you to easily do movement constrained by collisions without dealing with a Rigidbody, so we will not need to add a Collider or Rigidbody to the Player.
We will get a handle on the CharacterController.
Left & Right Movement
We are going to use Input.GetAxis(“Horizontal”), which is mapped to the left and right arrow keys. When pressing the left arrow key it will return a -1 and the right arrow key will return a 1. Then move create a Vector3 variable and place horizontalInput for the X and keep Y & Z to 0. Using the Character Contoller Move method which needs a Vector3 (direction).
The Player is moving pretty slow, so we want to increase the velocity, but first we need to create. Velocity is direct and speed. We already have the direction now we just need to speed and we want to be able to adjust it, so we will create a float variable and serialize it. We will create another Vector3 variable and make it the product of the direction we just calculate multiplied by the player speed variable. In the Move method, we replace direction with velocity.
Creating a gravity we will need to create a float variable and serialize it, so we can modify it. Gravity is basically the decreasing of the Y position, so we will access the Y in velocity and subtract the gravity value from it.
Gravity was decreasing the Y position and jumping is the inverse, it will be increasing the Y position. This will occur when we press the space bar. Lets create a jump height float variable and serialize. Before we use this, we want to only be able to jump if the player is on the ground. CharacterController has a isGrounded Method which will return true or false depending on if the CharacterController is detecting a collision with another object, which is the platform under the player. We will check to see if the controller is grounded and if it is then we will check to see if the space bar is pressed. If all of that is true with will add the jump height to velocity to the Y position and if not, we will subtract gravity from the Y position.
There is a problem. The jump increases the Y position by the jump height for a frame then it set back to 0. This is happening because the Y position (red arrow) starts at 0, when we press the space bar we add the jump height, lets say the jump height is 15, to the Y position (yellow arrow). It is now at 15 and we move the player (green arrow). Then it goes to the next frames and start back at recalculating the direction, which sets the Y back to 0 (red arrow).
We need to cache the Y velocity by creating a float variable and storing the Y’s change of position from the jump height and gravity. Right before the Move method is called we will set the velocity.Y to the cached current amount. This means when the frame is redrawn and the direction is being calculated, the Y will not be 0 but whatever the cache Y value is instead.
Make sure for the jump, we are no longer adding the jump height to the cache yVelocity but setting it equal to it.
That is the setup for the basic player movement in the platformer. Cheers!