Course contentsShow
← Unity Game Development
Lesson 189 of 8283. Physics and InteractionFree lesson

Implementing Variable Gravity for Jump Control

Learners will manipulate gravity scales during ascent and descent to create more responsive and satisfying platforming physics.

In this lesson, you will learn how to manipulate your player’s gravity scale in real-time to create a more responsive and "snappy" jumping feel.

The Problem with Constant Gravity

When using the default Unity physics, your character falls with the same intensity regardless of their current jump height. This often results in a "floaty" jump that feels heavy during the ascent and sluggish at the peak. To fix this, we want the player to feel weightless while they hold the jump button, but fall rapidly once they start heading back toward the ground.

Manipulating Gravity Scales

The Rigidbody2D component has a property called gravityScale. By modifying this value dynamically inside our FixedUpdate (or via an event-driven physics script), we can simulate a more precise vertical arc:

  • Ascent: When the player is moving upward, we can decrease the gravityScale slightly to allow for a higher, more controlled leap.
  • Descent: When the player begins falling, we increase the gravityScale beyond the default value. This makes the drop feel faster and more deliberate, removing that "floaty" feeling.

Here is how you can implement this logic based on the vertical velocity of your Rigidbody2D:

void Update()
{
    // If the player is falling, apply a heavier gravity multiplier
    if (rb.velocity.y < 0)
    {
        rb.gravityScale = fallMultiplier;
    }
    // If the player is jumping and releasing the button, apply less gravity
    else if (rb.velocity.y > 0 && !isJumping)
    {
        rb.gravityScale = lowJumpMultiplier;
    }
    else
    {
        rb.gravityScale = 1f;
    }
}

Why This Works

By adjusting these values, you are essentially changing the acceleration curve of your character. A higher multiplier during the fall creates a "snappier" landing, while a lower multiplier during the rise creates a feeling of precision. Since we are simply modifying the gravityScale on the Rigidbody2D component, it integrates perfectly with your existing physics-based movement and collision detection systems we’ve established in previous lessons.

Key Takeaway: By dynamically increasing your gravityScale during descent and decreasing it during ascent, you gain fine-tuned control over your character's jump arc, resulting in a tighter, more professional game feel.