Monday, July 15, 2024

Designing Platformer Player Controls

Taxonomy of Virtual Spaces Part II

One of the first steps in this project is to code a player avatar controller for 2-D platformer style gameplay, to allow the user to comfortably navigate the virtual environment. This also gives me a chance to use Unity's new Input System for the first time, as I've always used the Input Manager in previous projects.

Now, where to begin? Although I've created a few platformers in my time (like the experimental Super Mario Love (Rowe, 2019)), I've never created one professionally or for broader release. I've studied the game genre extensively and know enough to give my students advice on developing camera systems or how to adjust gravity to not feel "floaty," but this will be a new endeavor for me.


Building a Better Jump

Math for Game Programmers: Building a Better Jump (J. Kyle Pittman, 2016, GDC)

Building a Better Jump by J. Kyle Pittman is a lecture on using physics formulas to plan and design a platformer game controller, part of the "Math for Game Programmers" series of tutorials at the Game Developers Conference.

A common way to create a platformer game is to open the Unity editor, create a player character game object with a rigidbody, and write a script that applies a force on the object in an upward vector when the player presses the space bar. Then, through trial and error, the developer tunes the force value until the object can "jump" to the desired height. This tends to feel floaty, require a lot of iteration, and using Unity's default physics values tends to make it feel like every other Unity game out there.


How high?

Formulas for initial velocity and gravity

Pittman's approach is to remember your college physics formulas! By using the projectile motion vs. gravity formula f(t) = 1/2 gt^2 + v0t + p0 (Blogger is not great for writing formulas!), he shows that the initial velocity (v0)and gravity (g) values can be calculated based on how high you want the character to jump at the apex (h) and how much time you want the character to reach that apex (t). For example, if I want a character to jump 5 meters straight up and take 0.5 seconds to get there, then I should use a jump velocity of 20 m/s and a gravity value of 40 m/s^2 (or about 4 times our Earth's gravity).


How far?

Pittman uses more formulas to determine what lateral velocity (running speed) is needed to clear a certain jump distance. Remember calculating for how far a cannonball can fly before it impacts with the ground? No need to worry about launch angles here. Pittman shows how to calculate what x velocity is needed using time, height, and lateral distance requirements.


Break the Rules

Increase gravity at apex of jump to give the character a sense of weight

The formulas so far create a jump that is a perfect parabola, which unfortunately feels "floaty" and doesn't feel good to play. Here, Pittman quotes Game Feel (Swink, 2008), stating that in Super Mario Bros., the force of gravity triples at the apex of Mario's jump. (Swink: "At the apex of the jump, when velocity reaches 0, gravity is raised artificially by a factor of three, pulling Mario back to the ground with a much greater force than the one he overcame to get airborne in the first place" (2008, pg. 211). This "fast fall" technique makes the player feel "weighty" instead of "floaty." This is a simple technique that I've been teaching to my students for years.


Other modifications

Double jumping

Fast fall, double jumping, and variable-height jumps are all calculated using the same basic methods: use multiple parabolas or partial parabolas to determine the values needed. Pittman's methods give a great foundation for designing a platformer character controller from the ground up by the game's navigational requirements rather than fumbling around by trial and error.

There are still other important aspects of designing a platformer controller that are outside of scope of this lecture, such as coyote time, jump buffering, and hang time. Most of these involve setting timers and allowing the player to input a jump command when their character is not currently "grounded."

No comments:

Post a Comment

New Game Launched and Summer 2024 Research Review

Summer 2024 Review Taxonomy of Virtual Spaces Click here to play the new Taxonomy of Virtual Spaces - Expanded Edition interactive project...