Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 3)
Today I’m adding gravity, walking/running, and jumping to my 2D player controller created in my last post.

Today’s Objective: Build upon yesterday’s player controller to add the ability to move horizontally, run/sprint with shift, jump/double-jump, and fall with gravity.
The Code:



Now for the grounded and airborne behavior, which will be executed each frame from Update, depending on the grounded state.


To calculate the horizontal movement, I get the player input, calculate velocity using move speed, clamp, and if airborne, reduce the strength.

Jumping is simply applying upwards force to the vertical velocity, which is separate to velocity (for now) for ease of access in other areas.

Finally, the event methods. These are executed once when certain conditions are met.

Now my player can easily walk, run, jump, double-jump, and fall with gravity. Nice!