codeAPI Reference
s&box WaterVolume: Buoyancy physics for water volumes
WaterVolume API Reference
WaterVolume is a Component that applies buoyancy physics to Rigidbody objects entering a trigger volume.
Type Signature
CSHARP
public partial class WaterVolume : Component, Component.ITriggerListenerPhysics Implementation
CSHARP
protected override void OnFixedUpdate()
{
var collider = GetComponent<BoxCollider>();
var waterSurface = WorldPosition + Vector3.Up * (collider.Scale.z * 0.5f);
var waterPlane = new Plane(waterSurface, Vector3.Up);
foreach (var body in Bodies)
{
body.ApplyBuoyancy(waterPlane, Time.Delta);
}
}ITriggerListener Implementation
- OnTriggerEnter(Collider other) - Adds Rigidbody to the buoyancy list when it enters the trigger.
- OnTriggerExit(Collider other) - Removes Rigidbody from the buoyancy list when it exits.
Automatic Setup
GameManager automatically attaches WaterVolume to any collider tagged "water" after scene load:
CSHARP
void ISceneLoadingEvents.AfterLoad(Scene scene)
{
var waterVolumes = scene.GetAll<Collider>().Where(x => x.Tags.Has("water"));
foreach (var volume in waterVolumes)
{
volume.GetOrAddComponent<WaterVolume>();
}
}Notes
- Uses Rigidbody.ApplyBuoyancy(Plane, float) for physics simulation.
- Water surface is calculated from the BoxCollider's top face (Scale.z * 0.5).
- Invalid Rigidbodies are automatically removed from the tracking list.
- Tag your collider with "water" to enable automatic WaterVolume attachment.
Was this helpful?