2.5D Platformer — Basic Controller #2

Move, Jump and Fall!

As all Platformers, moving and jumping are the most common features in a controller, and most important of all, it should feel good and fun to control. Having dealt with platformers before, both 2D and 3D, I have some tips that might help you make something interesting.

Setup

With just a Character Controller in any of your GameObjects is fine. Once you have your controllable character, I added a script where I’ll code the movement. And yes, I use the new Input System and so should you.

Moving

To begin coding, I know that to move the player at a constant speed I’ll need a reference to the Character Controller to access its Move() method, an input value from our Movement Action Input, a walk speed variable, and a value where the final Horizontal Speed is stored and used to move the controller.

The Character Controller reference will be assigned in the Editor’s Inspector. To obtain the Input values, I’ve created a new Action Input called Movement, and in the Player Input component, I use Unity Events and added the method below which will run when any of the WASD keys are pressed. Because the input values returned by the System are abstract, I need to specify the type of value the controller is receiving.

Make sure that the Movement Input value isn’t normalized
I also use the Lamda Expression to write the necessary functionality in one line of code!

The Horizontal Speed is just the horizontal input value multiplied by the walk speed. And then, that value is used to move the controller. (Remember to multiply the velocity to Time.deltaTime to make the movement frame independent).

The horizontal movement is Done! However, we should be able to fall.

Falling

To fall down, it’s as easy as constantly adding the gravity acceleration to the Current Vertical Speed, clamping the max negative speed, and then applying that to the total velocity of the controller.

But, I found it to be snappier if the controller fell faster than it rises up (you’ll see when I get to jumping). So, I applied extra force only if the Current Vertical Velocity is lower than 0.

Now, it falls. But there’s a little issue on how it actually falls. More on that in a bit.

Jumping

As almost all jumping mechanics, to jump the controller will need to be on the ground, apply the jump force to the controller, and restrict the player from jumping again until the controller touches the ground.

This is the method which checks if the Controller is grounded or not.

To handle reseting the “jumped” boolean and adjusting some values to my liking, such as not instantly falling at the gravity velocity, I’ve declared a Grounded getter/setter. Every time the previous value doesn’t match the new one, an event is triggered.

What does “OnGroundStateChange” do? It’s an event that will invoke all suscribed methods to it. In this case, its suscribed to the method below.

Why not change the Vertical Speed to 0 when grounded? The Character Controller has some “weird” design decisions and, unless the Character Controller isn’t moving, it won’t update the .IsGrounded variable. Mildly annoying.

And last, but not least, I’ve updated the VerticalMovement method to add gravity acceleration only when the controller isn’t on the ground.

End Result

--

--

--

Junior Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Day 24 — Satisfied

Don't miss this project

How Airbnb use AWS?

Introduction to AWS RoboMaker

Clean Up Your Spock Tests

Choosing an Analytics Tool. Metabase Vs Superset Vs Redash

Why should I choose Python for web development?

Lab 10: Musical Instrument

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
Pablo Gómez Platón

Pablo Gómez Platón

Junior Unity Developer

More from Medium

2D Player Animation (part 2)

Creating Coin Distraction Feature in Unity (Part 1)

2D Mobile: Loot System

Cross-platform Input Controller