Geek Culture
Published in

Geek Culture

2.5D Platformer: Character Controller, Part 1

In this article, I’ll be setting up a physics-based character controller for the Player through the script. The Player will have horizontal movement, gravity, jump, and double jump all through scripting.

First, I’ve set up a very basic level using primitives. I have a few platforms, some spheres to act as collectibles, and a capsule for the player.

To set up the player, first, I will remove the Capsule Collider since the Player will not use it. Then I’ll add a Character Controller component.

This will allow me to have complete control of the Player’s movement, including velocity and gravity. This will be done through a script named Player that will be attached to the Player GameObject.

Now it’s time to open up the script and start setting up some values. To start, I need to get a handle on the Character Controller component on the Player. Then I assign the handle to GetComponent<CharacterController>().

Also, since I already know I’m going to want a value for gravity, jump height, and the player speed, I’m going to go ahead and set those up as well. So those will be good to start with, and I’ll be adding more later as needed.

Now I can start setting up the scripting to move the character. First, I’ll get the horizontal input using Input.GetAxis and assign it to a float named horizontalInput, then, I’ll define a new Vector3 direction based on the input, and finally, using the Character Controller Move() method, I’ll move using that direction.

This will give a nice, albeit slow, movement for the Player, as shown below.

Now I’ll add a bit of speed to the Player. To do this, I’ll set the _playerSpeed to 5.0f. Then, in the Update() method, right after setting the Vector3 direction, I’ll set a new Vector3 named velocity and set it to the direction * _playerSpeed. Then in the Move() method, I’ll use the new velocity instead of direction.

This gives a much nicer speed for the Player movement, as you can see below.

That covers the first part of setting up a Character Controller for the player. In the next article, I’ll be adding Gravity, Jump, and Double Jump. So make sure to check back for more coding fun.

And, as always, I wish you all the best on your own coding journey.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store