Skip to main content
Game Physics – Complete Beginner to Advanced Guide
CHAPTER 04 Intermediate

Gravity and Falling Systems

Updated: May 16, 2026
25 min read

# CHAPTER 4

Gravity and Falling Systems

1. Introduction

Gravity is the anchor of the virtual world. Without it, characters float away into the void. However, programming gravity is not as simple as subtracting a set amount from the Y-axis every frame. True gravity is an *acceleration*—the longer you fall, the faster you go, until you hit a maximum speed known as terminal velocity. Furthermore, in genres like Platformers, strict realism feels terrible; jumps must be snappy, controllable, and responsive. In this chapter, we will master Gravity and Falling Systems. We will learn the math behind gravitational acceleration, implement a dynamic jump arc, and cap our falling speed.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Differentiate between constant falling speed and gravitational acceleration.
  • Implement a basic gravity system using vertical velocity.
  • Code a responsive jump mechanic based on an initial impulse.
  • Implement Terminal Velocity to prevent game-breaking physics glitches.
  • Understand "Arcade" gravity tuning for platformer games.

3. Gravitational Acceleration (The Math)

If you drop a rock, it doesn't fall at a steady 5 mph. It starts at 0 mph, then 10 mph, then 20 mph. Gravity increases Velocity over time.
  • Standard Earth gravity is approximately -9.81 m/s².
  • In code, we must continually subtract this value from our vertical velocity (velocity.y).
csharp
1234567891011
float gravity = -9.81f;
Vector3 velocity;

void Update()
{
    // Apply gravity to the vertical velocity over time
    velocity.y += gravity * Time.deltaTime;

    // Apply the resulting velocity to the actual position
    transform.position += velocity * Time.deltaTime;
}

4. The Jump Mechanic (The Impulse)

Jumping is essentially fighting gravity with a massive, instantaneous burst of upward velocity.
  • When the player presses 'Jump', you instantly set velocity.y to a high positive number.
  • In the following frames, gravity will automatically chip away at that positive number, slowing the player down at the apex of the jump, and eventually pulling them back down.
csharp
1234567
float jumpForce = 5.0f;

if (Input.GetKeyDown("space") && isGrounded)
{
    // Instantly overwrite the vertical velocity
    velocity.y = jumpForce; 
}

5. Terminal Velocity (The Speed Limit)

If a player falls off a massive cliff, gravity will keep accelerating them endlessly. If they reach 10,000 mph, they will instantly teleport through the ground collision box because they moved too far in a single frame (Tunneling).
  • You must enforce Terminal Velocity: a maximum falling speed.
csharp
12345678910
float terminalVelocity = -50.0f;

// Apply gravity
velocity.y += gravity * Time.deltaTime;

// Clamp the falling speed to prevent tunneling
if (velocity.y < terminalVelocity)
{
    velocity.y = terminalVelocity;
}

6. Arcade Tuning (The "Mario" Jump)

Real gravity feels "floaty" in video games. To make a platformer feel responsive:
  1. 1. High Gravity, High Jump: Double the gravity to -20f so the player falls fast, and double the jump force to compensate. This makes the jump feel "snappy."
  1. 2. Variable Jump Height: If the player lets go of the jump button early, artificially multiply the gravity or cut the velocity.y in half. This allows for short hops versus high jumps.

7. Visual Learning: The Jump Arc

txt
1234567
      (Apex: velocity.y = 0)
            ____
          /      \
(v.y = 5)/        \ (v.y = -5)
        /          \
[ JUMP ]            [ LAND ]
(v.y = 10)          (v.y = -10, reset to 0)

8. Best Practices

  • Resetting Velocity on Landing: The moment your character touches the ground, you MUST set velocity.y = 0 (or a very small negative number like -2f to keep them stuck to the floor). If you let gravity build up while standing on the floor, the moment the player walks off a ledge, they will shoot downward at Mach 3!

9. Common Mistakes

  • Grounded Checks: The hardest part of jumping is writing the isGrounded boolean. If your code thinks the player is slightly in the air because of a bumpy floor, they won't be able to jump. Always use a generous Raycast or a SphereCast pointing downward from the player's feet to reliably detect the floor.

10. Mini Project: Build a Custom Gravity Controller

Objective: Write a script that handles jumping, gravity acceleration, and landing.
csharp
123456789101112131415161718192021222324252627282930313233
class GravityController
{
    public float gravity = -20.0f; // Snappy arcade gravity
    public float jumpVelocity = 8.0f;
    Vector3 currentVelocity;
    bool isGrounded = true;

    void Update()
    {
        // 1. Check if on ground (Conceptual method)
        isGrounded = CheckGround(); 

        if (isGrounded && currentVelocity.y < 0)
        {
            // Reset gravity buildup when standing on the floor!
            currentVelocity.y = -2f; 
        }

        // 2. Jump input
        if (Input.GetKeyDown("space") && isGrounded)
        {
            currentVelocity.y = jumpVelocity;
        }

        // 3. Apply Gravity Acceleration
        currentVelocity.y += gravity * Time.deltaTime;

        // 4. Apply Final Movement
        transform.position += currentVelocity * Time.deltaTime;
    }

    bool CheckGround() { /* Perform Raycast downward */ return true; }
}

11. Practice Exercises

  1. 1. Why must gravity be added to velocity over time, rather than added directly to position?
  1. 2. What is Terminal Velocity, and what critical physics bug does it prevent?

12. MCQs with Answers

Question 1

When programming a jump in a custom kinematic controller, what mathematical action should you take the exact moment the player presses the jump button?

Question 2

A character stands on a platform for 10 minutes. When they step off the edge, they instantly teleport to the bottom of the level at a million miles an hour. What code did the developer forget to write?

13. Interview Questions

  • Q: Explain the mathematical difference between linear movement (speed) and gravitational movement (acceleration). How is this represented in code?
  • Q: What is "Variable Jump Height" (where holding the jump button goes higher than tapping it)? Explain the logic used to achieve this arcade physics mechanic.
  • Q: Describe the concept of "Tunneling" in physics engines. How does enforcing a Terminal Velocity mitigate this issue for fast-falling objects?

14. FAQs

Q: Can't I just use Unity's Rigidbody to handle gravity? A: Yes! A Rigidbody handles all of this automatically. However, for 2D platformers (like *Hollow Knight* or *Super Meat Boy*), developers almost always write custom kinematic gravity scripts like this because built-in physics engines feel too "floaty" and unpredictable for precise platforming.

15. Summary

In Chapter 4, we grounded our world. We learned the difference between flat speed and accelerating forces. We programmed realistic gravity by modifying velocity over time, and crafted explosive jumps by applying instantaneous vertical impulses. We protected our simulation from the dreaded "Tunneling" bug by enforcing Terminal Velocity speed limits, and learned the crucial rule of resetting accumulated gravity when touching the floor.

16. Next Chapter Recommendation

Our character falls perfectly, but right now they fall straight through the floor! We need solid geometry. Proceed to Chapter 5: Collision Detection Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·