# How can I implement physics objects which float in water?

I hope this is the right place for my question! I would like to know if the following is possible using the Unity engine:

The scenario is a rising pool of water.

On the water are floating objects, like crates (some are already floating on the surface, others fall into the water from above).

As the water is rising, I want the player (3rd person) to be able to jump from crate to crate, with the crates responding to the impact, but staying afloat.

I would like the crates to be affected by other NPCs or forces in the world, like fish or water shooting out of the player's Super Soaker gun.

Does any of this sound possible? Any answers greatly appreciated!

Yep, it's certainly possible (I have done this), although the physics of objects floating in water isn't built-in to unity's physics engine. You have to apply the forces to keep objects afloat yourself, based on how much of the object is underwater - however it's not as simple as that might first sound.

You can't just apply an upwards force based on how far underwater the object is, because that doesn't take into account that an object will attempt to lie flat based on its geometry.

I implemented buoyancy by pre-calculating a sort of volumetric grid of points that are inside the volume of the floating object, giving a sort of low-res representation of the volume of the object.

Then, when updating, I was checking how many of the volume-points are below the waterlevel. The more that were below the waterlevel, the harder the upwards force on the object. However, the point at which the force should be applied also has to be calculated, since if one end of your object is dipping further down in the water, that end gets pushed up harder.

The way I solved this was to average the position of only those volume-points which are below-waterlevel, and then use that average position as the point at which to apply the upwards buoyancy force.

This upwards force was also multiplied by a factor which determined how buoyant the object should be. This made some objects only float with a small portion above water, while very buoyant objects almost sit entirely on top of the water.

Once this was working, I was able to make floating platforms which supported my player character, and even bobbed realistically if you jumped up and down on a corner!

Check these two forums threads (Buoyancy1, Buoyancy2). They contain examples of buoyancy scripts which might get you started implementing what Duck suggested.