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.

Walking, running, jumping and double-jumping.

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:

If I don’t forcefully stop input into a wall, the position is unstable.

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!

--

--

--

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

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

Recommended from Medium

How Apache Kafka works? Kafka Streams Series-3

Geo University: a new and interactive platform to learn GIS and Earth Observation

Vulcan Forged is getting ready for the champions league of crypto.

Whose Value is it Anyway?

Microsoft learning pathways for Admins (2020)

A balding middle-aged man sits in front of a bank of computers and screens

RestFul server with Harbour

Build Your Personal Brand with a Profile Page using GitHub Pages and Hugo

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
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

More from Medium

Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 2)

Making a 2.5D Character Controller in Unity Part 1

Drone Controls w/ New Unity Input System

Unity Observers