Floating Body
Floating Body is a ready-to-use example implementation of Water Behaviour, that works on top of Unity's RigidBody.
Create new game object
Add Unity's RigidBody
Set proper mass and drag values (try to use possibly real mass value with high drag - both, drag and angular drag at value 1 give decent results and are good starting point for futher tweaking).
Set up colliders - just make entire rigidbody as usual
Add FloatingBody
component
Water Sampler Config
This section comes from base Water Behaviour component and it is used to configure internal Water Sampler.
Precision - amount of iterations used to sample water height, in most cases 3-4 is more than enough.
Frame Divider - water will be sampled every N-th frame. If you have many objects with e.g. N=2, then they will automatically split calculations between odd and even frames to save extra performance.
Surface Detection - how water surfaces to sample will be acquired.
Default - if you have configured Default
Water Surface, then only this surface will be used. Otherwise it falls back to Auto Cull.
Auto Cull - automatically detects nearby surfaces.
Custom - manually provide list of surfaces that will be sampled. Recommended option if you have multiple surfaces, especially if you know exactly what surface your body will be floating on and you can change it runtime.
Cutout Detection - how cutout volumes used to exclude from surface sampler will be acquired.
Auto Cull - automatically detects nearby cutout volumes.
Dont Cutout - completely skip cutout detection and always sample surfaces. This is the most performant option and is highly recommended if you don't care about excluding cutout volumes from water sampling, which is done via collider raycasting.
Custom - manually provide list of cutout volumes that you want to consider. Recommended option if you can easily fill it.
Auto Culling Box - if enabled, bounding box used to detect nearby cutout volumes and water surfaces will be calculated automatically. Otherwise you will have to manually provide box size, which is recommended to skip some calculations.
Geometry Settings
Here you configue actual shape of body that will be used for the buoyancy physics.
It consists of two parts:
Center of Mass - orange gizmo dot, self-explantatory.
Contact Points - yellow gizmo dots, points on your body's shape that will be actually sampled again water and where final buoyancy force will be applied.
Every body is unique and may need different recipe how to configure its floating geometry.
However, here's a few general tips for most body types like ships:
Center of mass should be at the bottom of the body, even below all contact points for better stability.
Don't put too many points to save performance - for small bodies 1-4 points is enough, for large bodies 10 to 30 should work.
Place contact points on hull, not inside body.
Contact points are responsible for force distribution, so keep constant density of them over entire body - if you put 5 on front then also put 5 on back instead of 20.
As above, make contact points symmetrical - right side of ship should be equal to left side for stable buoyancy.
Place contact points over entire body at different height levels, not just one.
Editing Geometry
To edit geometry, toggle Edit Mode button. You can edit center of mass and contact points in two ways:
Simple Disc Placer
This is primitive tool that may help you initially place contact points for faster prototyping.
It uses a XZ plane disc to raycast N points towards the middle of body at specified height level.
Manipulate with radius, amount of samples and angle offset to quickly place points evenly on hull.
Press Add to add newly scanned points and Clear to clear everything at given height level.
Preview raycast drawn as gizmo will be helpful.
Repeat it few times at different height levels.
This is just prototyping tool. In the end you should do final touches manually to make all contact points with equal and symmetrical offsets.
Force
Here you configure actual buyonace force.
This force is evenly splitted for every contact point and applied on it individually if given point is submerged.
Buoyancy Force - amount of buoyancy force.
Normal Alignment - how much wave normal will affect applied buyonacy force. It is useful if you have like only one contact point to achieve more realistic floating.
Instability - scales with normal alignment. By default, Floating Body adds extra counter-force to neutralize self-moving body and keep it stable on water. Higher instability mean that body will move with wave direction, negative means opposite.
NOTE: Buoyancy force is additionaly multiplier by factor defined by Water Surface that body is floating on.
Submerge Drag
Rigidbody should have more drag in water than in air.
Here you can add extra Drag and Angular Drag to rigidbody when its fully submerged. Applied drag is scaled by submerge level.
Submerge level is a percentage value determined by amount of contact points that are below water.
NOTE: There are also extra drag values added by Water Surface itself, where you can configure it too.
NOTE: Submerge drag values are not actually added to rigidbody drag values, but are calculated independently, so you can still change your rigidbody values runtime as you want without breaking floating body state.