Day 13: Creating a 2.5D Platformer game with unity physics

Objective: To create a 2.5D platforming game similar to that of super mario. We will first create our character controller.
Now that the shooting game is complete, I am moving on to a 2.5D platformer that will feature many gameplay elements seen in games like Mario, Sonic, Rayman, Crash Bandicoot and the list goes on.
Unity comes with its own Physics based character controller with methods already provided to get us started. I write a player script that will hook into this controller and create the gameplay I’m looking for.

In this player script we hook into the Character Controller component and program the movement for the player.

With a connection to the character controller that Unity has provided for us, I can now use the public method provided by MonoBehaviour. I create a local variable inside update that reads the float value of the “Horizontal Axis”(which is typically the ‘A’ and ‘D’ key). We will assign this float value to the X position to a new vector 3. The public method Move requires a Vector 3 and an extra paramter I havent defined yet. So now we can move left and right. The next step if the Jump mechanic.

first we need to create gravity. to apply gravity to the character we will subtract gravity from the Y value of the Vector3 I created called _velocity. Now we have gravity so its time to create the jump mechanic. If the player’s character controller is grounded then the player can jump with the space key.


Now this will produce a weird effect where the player jumps in a snappy fashion which is not desirable. We will cache the y value of the _direction Vector 3. Caching the y value by having it take a snapshot of the players y velocity each frame and assigning it to the cached _yVelocity float. This helps me to achieve a nice foundation for our jumping mechanic.
