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. When entering the Trigger, save the object's original Linear Drag value.
-
2.
Increase the Rigidbody's Linear Drag to a high number (e.g.,
5.0).
-
3.
When leaving the Trigger (
OnTriggerExit), restore the original Drag value.
csharp
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
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
AddForcein a specific direction usingForceMode.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
8. Best Practices
-
Limit OnTriggerStay Math:
OnTriggerStaycan 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
upwardForcemultiplier 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'sdragandmassto 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
11. Practice Exercises
- 1. What built-in Unity/C# method is called continuously every physics frame while an object remains inside a Trigger collider?
- 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
OnTriggerStayloop 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
ForceModeparameters.
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 Rigidbodydrag 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.