Boats, crates, barrels and a swimming player all need the same thing: water that pushes back. Unity has no built-in water physics, so you build it from three parts: a trigger volume that marks the water, a script that applies upward force to bodies inside it, and extra drag so things settle instead of bouncing forever. This guide shows how to build Unity water buoyancy for floating objects, then adds a simple swim state for a player character.
How Buoyancy Works
In real physics, a submerged object is pushed up with a force equal to the weight of the water it displaces. In a game, we fake it with one rule: the deeper an object sits below the surface, the harder it is pushed up.
- Above the surface, do nothing. Gravity handles it.
- Below the surface, add an upward force proportional to depth.
- Also increase drag while submerged, so movement slows down like moving through water.
An object floats where the upward force balances gravity. Heavier objects sink deeper before that happens, and a strong enough upward force makes them rise out of the water and fall back, which gives the natural bobbing.
All of this runs on a Rigidbody, so the force fundamentals matter. If your forces seem to do nothing, check the AddForce not working guide first.
The Water Volume
Create a Cube, scale it to cover the water area, and set it up like this:
- Tick Is Trigger on its Box Collider.
- Turn off the Mesh Renderer (or replace it with a real water surface, such as the one in the water shader graph guide).
- Add the
WaterVolumescript below.
The top face of the box is the water surface. The script exposes the surface height so other scripts can ask for it:
using UnityEngine;
[RequireComponent(typeof(BoxCollider))]
public class WaterVolume : MonoBehaviour
{
BoxCollider box;
void Awake()
{
box = GetComponent<BoxCollider>();
box.isTrigger = true;
}
// World-space Y of the water surface (the top of the box)
public float SurfaceY => box.bounds.max.y;
}
Using bounds.max.y means you can move or scale the volume and the surface follows. For waves, replace SurfaceY with a method that takes a position and returns a height, such as GetSurfaceY(Vector3 worldPos) using Mathf.Sin. The floating code does not need to change.
A Floating Object
Add this to any object with a Rigidbody and a collider. It finds the water through the trigger and applies the force each physics step:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Floater : MonoBehaviour
{
[SerializeField] float buoyancy = 20f; // upward strength per unit of depth
[SerializeField] float maxDepth = 1f; // depth at which force stops growing
[SerializeField] float waterDrag = 2f;
[SerializeField] float waterAngularDrag = 1f;
Rigidbody rb;
WaterVolume water;
float airDrag, airAngularDrag;
void Awake()
{
rb = GetComponent<Rigidbody>();
airDrag = rb.linearDamping;
airAngularDrag = rb.angularDamping;
}
void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out WaterVolume w)) water = w;
}
void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out WaterVolume w) && w == water)
{
water = null;
rb.linearDamping = airDrag;
rb.angularDamping = airAngularDrag;
}
}
void FixedUpdate()
{
if (water == null) return;
float depth = water.SurfaceY - transform.position.y;
if (depth <= 0f) return;
float submerged = Mathf.Clamp01(depth / maxDepth);
rb.AddForce(Vector3.up * (buoyancy * submerged * rb.mass), ForceMode.Force);
rb.linearDamping = waterDrag;
rb.angularDamping = waterAngularDrag;
}
}
In Unity 6, the Rigidbody properties are linearDamping and angularDamping. The old drag and angularDrag names are obsolete. Note that Unity calls trigger messages on the Rigidbody’s object, and a trigger collider and a Rigidbody are needed on at least one of the two objects. The floating object has the Rigidbody, so this works.
Multiplying by rb.mass makes the result mass-independent: with buoyancy at about twice gravity (9.81 x 2 is roughly 20), the object rises when fully submerged. Set buoyancy near 9.81 and the object hovers. A value between 1x and 2x gravity gives an object that floats with part of it above the water. Tune buoyancy and maxDepth by playing the scene and watching one crate. Raise waterDrag if it bounces too long.
Multiple Float Points for Boats
With one force at the centre, a boat can float but will never tilt with the waves, because force at the centre never creates torque. Put several float points on the hull, such as four corners, and apply force at each one:
[SerializeField] Transform[] floatPoints;
void FixedUpdate()
{
if (water == null) return;
int count = floatPoints.Length;
foreach (Transform p in floatPoints)
{
float depth = water.SurfaceY - p.position.y;
if (depth <= 0f) continue;
float submerged = Mathf.Clamp01(depth / maxDepth);
Vector3 force = Vector3.up * (buoyancy * submerged * rb.mass / count);
rb.AddForceAtPosition(force, p.position, ForceMode.Force);
}
}
Dividing by the point count keeps the total force the same as the single-point version. Each corner now pushes independently, so the boat tilts when one side is lower and rights itself when it is level. Lower the Rigidbody’s Center of Mass (set rb.centerOfMass) so the boat resists capsizing. Use a simple box or capsule collider for the hull rather than a detailed mesh collider.
Player Swimming
Swimming is a movement state: when the player’s chest is below the surface, switch from walking physics to swim physics. Here is a version for a Rigidbody player. If you use a CharacterController, the same state logic applies but you move with Move and apply your own gravity.
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
public class PlayerSwim : MonoBehaviour
{
[SerializeField] float swimSpeed = 3f;
[SerializeField] float verticalSpeed = 2f;
[SerializeField] float swimDepth = 1f; // how deep before we swim
[SerializeField] Transform cameraTransform;
Rigidbody rb;
WaterVolume water;
public bool IsSwimming { get; private set; }
void Awake() => rb = GetComponent<Rigidbody>();
void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out WaterVolume w)) water = w;
}
void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out WaterVolume w) && w == water) water = null;
}
void FixedUpdate()
{
IsSwimming = water != null && (water.SurfaceY - transform.position.y) > swimDepth;
rb.useGravity = !IsSwimming;
if (!IsSwimming) return;
Vector2 move = Keyboard.current != null
? new Vector2(
(Keyboard.current.dKey.isPressed ? 1 : 0) - (Keyboard.current.aKey.isPressed ? 1 : 0),
(Keyboard.current.wKey.isPressed ? 1 : 0) - (Keyboard.current.sKey.isPressed ? 1 : 0))
: Vector2.zero;
float vertical = 0f;
if (Keyboard.current != null)
{
if (Keyboard.current.spaceKey.isPressed) vertical += 1f;
if (Keyboard.current.leftCtrlKey.isPressed) vertical -= 1f;
}
Vector3 dir = cameraTransform.forward * move.y + cameraTransform.right * move.x;
Vector3 target = dir * swimSpeed + Vector3.up * (vertical * verticalSpeed);
// ease toward the target for a floaty feel
rb.linearVelocity = Vector3.Lerp(rb.linearVelocity, target, 5f * Time.fixedDeltaTime);
}
}
While swimming, gravity is turned off and the player moves in the camera’s direction, with Space and Left Ctrl for up and down. When leaving the water or reaching the surface, gravity returns. Reading keys in FixedUpdate can miss one-frame presses, but holding keys with isPressed is fine here. Move the actual input reading into Update if you add a button press such as a dive. For a cleaner setup, use an Input Actions asset instead of reading keys directly.
Make the player stop swimming slightly below the surface, using swimDepth, so the head stays above water and the player does not flicker between states at the surface. For underwater visuals, switch on a blue Volume or fog when the camera’s Y is below SurfaceY, and low-pass the audio. A body that falls into water at high speed can also use a short extra damping burst so it does not shoot straight to the bottom. Ragdolls float the same way: add a Floater to the main bone, as in the ragdoll tutorial.
Polish: Splashes and Sound
- Splash: in OnTriggerEnter, spawn a particle system at the contact point, scaled by
rb.linearVelocity.magnitude. - Sound: play a splash clip on enter and on exit. Louder for faster entries.
- Ripples: spawn a ring sprite or decal on the surface at the object’s x and z.
- Underwater look: enable fog or a post-processing Volume with blue tint when the camera is under the surface.
🔧 Water and effects assets
Ready-made water surfaces and splash effects can save days. Browse our recommended Asset Store tools.
Troubleshooting
The object sinks straight through the water
The upward force is smaller than gravity. Raise buoyancy, or check that the Floater is receiving OnTriggerEnter. A trigger needs a Rigidbody on one of the two objects and Is Trigger ticked on the water collider.
OnTriggerEnter never fires
The water collider is not a trigger, or the layers do not collide in the Layer Collision Matrix. Also check that the script sits on the object with the Rigidbody or its collider.
The object shoots out of the water
The force is too strong or there is not enough drag. Lower buoyancy, raise waterDrag and use a Clamp on submerged depth.
The object bobs forever
Not enough damping. Raise linearDamping while submerged, and add a little extra vertical damping when in water.
CS0619 or a warning that ‘Rigidbody.drag’ is obsolete
Use linearDamping and angularDamping in Unity 6.
The player flickers between swimming and walking at the surface
Add a depth threshold and a small hysteresis, entering swim state a bit deeper than the exit point.
FAQ
Do I need a physics engine for water?
No. The depth-based upward force is enough for crates, barrels and boats. Full fluid simulation is overkill for most games.
How do I make waves affect floating objects?
Replace SurfaceY with a function that returns the wave height at a given position, and use the same function in your water shader so visuals match.
Does this work in 2D?
Yes. Use Rigidbody2D, OnTriggerStay2D and AddForce with Vector2.up. The 2D body uses linearDamping too.
Why does my boat spin?
Forces applied off-centre create torque. Keep float points symmetrical, increase angular damping, and lower the centre of mass.
Can I use a mesh collider for the water?
A Box Collider is simpler and faster. Use a convex mesh trigger only if the water has an odd shape.
Reader questions
Asked by readers of this article and answered by AI. Behavior can differ between Unity versions, so test in yours.




Leave a Reply