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

Water, Fluids, and Environmental Physics

Updated: May 16, 2026
30 min read

# CHAPTER 13

Water, Fluids, and Environmental Physics

1. Introduction

Physics engines excel at rigid bodies—solid blocks of stone or metal bouncing off each other. But the real world is filled with fluid dynamics: wind rushing through valleys, water resisting movement, and objects bobbing in the ocean. True volumetric fluid simulation is incredibly CPU-intensive and rarely used in real-time games. Instead, game developers use clever mathematical tricks to *fake* fluid interactions. In this chapter, we will master Environmental Physics. We will learn how to program Buoyancy (floating), implement dense underwater drag, and apply directional wind forces using Trigger volumes.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand how games fake water using Trigger Volumes.
  • Calculate and apply upward Buoyancy forces based on submersion depth.
  • Dynamically alter a Rigidbody's drag to simulate water resistance.
  • Create localized wind/tractor-beam zones.
  • Optimize environmental physics calculations.

3. Faking Water (The Trigger Volume)

Water in a game is simply a giant, invisible Box Collider set to "Is Trigger", with a wobbly blue texture painted over it.
  • When an object enters the trigger (OnTriggerEnter), it is officially "underwater."
  • While inside, the physics rules change.

4. Simulating Drag (Water Resistance)

The moment a car drives into a lake, it should slow down immensely.
  1. 1. When entering the Trigger, save the object's original Linear Drag value.
  1. 2. Increase the Rigidbody's Linear Drag to a high number (e.g., 5.0).
  1. 3. When leaving the Trigger (OnTriggerExit), restore the original Drag value.
csharp
1234567891011
void OnTriggerEnter(Collider other)
{
    Rigidbody rb = other.GetComponent<Rigidbody>();
    if (rb != null) rb.drag = 5.0f; // High water resistance
}

void OnTriggerExit(Collider other)
{
    Rigidbody rb = other.GetComponent<Rigidbody>();
    if (rb != null) rb.drag = 0.1f; // Normal air resistance
}

5. Buoyancy (Floating Math)

To make a wooden crate float, you must apply a continuous upward force (Archimedes' principle) that counteracts gravity.
  • The force should be stronger the deeper the object goes.
  • In OnTriggerStay (which runs every physics frame the object is inside the water), calculate the distance from the object to the water's surface.
csharp
12345678910111213141516171819
public float upwardForce = 20f;
public float surfaceYLevel = 5.0f; // The top of the water

void OnTriggerStay(Collider other)
{
    Rigidbody rb = other.GetComponent<Rigidbody>();
    if (rb != null)
    {
        // How deep is it? (Surface - Object Position)
        float depth = surfaceYLevel - other.transform.position.y;
        
        // Only apply upward force if it's actually submerged
        if (depth > 0)
        {
            // The deeper it goes, the harder it pushes up!
            rb.AddForce(Vector3.up * upwardForce * depth);
        }
    }
}

6. Wind and Directional Forces

You can use the exact same Trigger Volume logic to create a wind tunnel, a fan, or a sci-fi tractor beam!
  • Instead of calculating depth, you simply apply a steady AddForce in a specific direction using ForceMode.Acceleration.
  • By applying this in OnTriggerStay, any physics object (a player, a bullet, a crate) that enters the invisible box will be blown away automatically.

7. Visual Learning: The Buoyancy Curve

txt
123456
Water Surface (Y = 5) ------------------------- (Force = 0)
                                |
Depth 1m (Y = 4)                v               (Force = 20)
                                |
Depth 2m (Y = 3)                v               (Force = 40)
*The deeper the crate sinks, the more violent the upward bounce!*

8. Best Practices

  • Limit OnTriggerStay Math: OnTriggerStay can be very expensive if 500 rocks fall into the ocean. Instead of applying complex buoyancy math to every pebble, only apply full buoyancy to large objects (crates, boats), and simply let tiny rocks sink to the bottom by just applying high drag.

9. Common Mistakes

  • Exploding Buoyancy: If your upwardForce multiplier is too high, the moment a crate hits the water, the math will calculate a massive upward force. The crate will launch out of the water, fly 100 feet into the air, fall back down, and repeat endlessly. Always balance the upward force against the Rigidbody's drag and mass to create a smooth, settling bob.

10. Mini Project: Build an Anti-Gravity Zone

Objective: Create a zone that inverts gravity for anything inside it.
csharp
12345678910111213141516
class AntiGravityZone : MonoBehaviour
{
    public float antiGravityForce = 19.62f; // Double standard gravity!

    void OnTriggerStay(Collider other)
    {
        Rigidbody rb = other.GetComponent<Rigidbody>();
        if (rb != null)
        {
            // Standard gravity pulls down at -9.81.
            // By pushing up at 19.62, the object will gently float upward!
            // We use Acceleration so it ignores the object's Mass.
            rb.AddForce(Vector3.up * antiGravityForce, ForceMode.Acceleration);
        }
    }
}

11. Practice Exercises

  1. 1. What built-in Unity/C# method is called continuously every physics frame while an object remains inside a Trigger collider?
  1. 2. To simulate water resistance instantly upon hitting the water, which specific Rigidbody property should you drastically increase?

12. MCQs with Answers

Question 1

When programming a realistic buoyancy system for a boat, the upward force applied by the water should be multiplied by what specific value?

Question 2

You are building an environmental wind tunnel. Any object inside the tunnel should be pushed smoothly to the right, regardless of whether it is a 1kg feather or a 1000kg car. Which ForceMode should you use in your AddForce method?

13. Interview Questions

  • Q: Explain the programmatic architecture of a body of water in a game engine. How do Trigger Volumes and the OnTriggerStay loop interact to fake fluid dynamics?
  • Q: Walk me through the mathematical implementation of Archimedes' Principle (Buoyancy) in C#. How do you balance the upward force to prevent the object from launching infinitely into the sky?
  • Q: A developer complains that their wind tunnel pushes light boxes perfectly, but heavy cars don't move at all. Explain the relationship between Force and Mass, and how to bypass Mass using specific ForceMode parameters.

14. FAQs

Q: Can I simulate real splashing and pouring water? A: Real-time fluid simulation (Navier-Stokes equations) is incredibly heavy. Some AAA games (like *Borderlands*) use Nvidia Flex particles to simulate pouring fluids, but for indie games, it is almost always faked using visual shaders and simple particle systems, not actual physics.

15. Summary

In Chapter 13, we expanded our physics knowledge into the environment. We learned how to fake complex fluid dynamics using invisible Trigger Volumes. We manipulated the Rigidbody drag property to simulate dense water resistance, and wrote dynamic mathematical algorithms to calculate depth-based buoyancy forces. We repurposed this logic to create wind tunnels and anti-gravity zones. The game world is now a tactile, reactive environment.

16. Next Chapter Recommendation

Running physics on one computer is hard. Running it perfectly synchronized on 10 computers across the internet is a nightmare. Proceed to Chapter 14: Multiplayer Physics and Synchronization.

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: ·