Gravity Force That Works Like Water Surface

Hello
We’re making a 2D game that features a controllable fish game object that can swim around and go up until it reaches the surface of the water (the uppermost part of a blue sprite representing the water).
We’re trying to implement a force that pulls the fish down so that it cannot go out of water unless it is fast enough to get through the surface before going back again to the water by the gravity force.

Tips and insights are appreciated.

I think you have actually just explained the entire problem to yourself… you need to add a downward force to the fish based on how far out of the water it is!!

A neutrally-buoyant fish begins to feel a force back into the water as soon as any part of him rises above the water surface.

When his entire body is not in water, then the force downward is his full weight, technically minus the mass of air he displaces.

A simplification is to consider the entire fish concentrated at his central point. This simplification breaks down if the fish is very large or moving very slowly compared to his length, but for little minnows bolting all over, it’s a pretty good simplification.

You could also use the in-built BuoyancyEffector2D. Enclosed find my sample project… no code, just buoyancy effectors.

You would trim your fish (change its mass) so that it is neutrally buoyant in the effector.

I am also going to page @MelvMay because his job is in this 2D Physics stuff and well, because he’s just a great guy to have around in these threads. :slight_smile:

EDIT: I added some code and made a SphereFish (caution: this is an endangered species!) that can fly around and leap out of the water, and even flop around once out of water.

7891798–1004578–Water2DBuoyancyEffector2D.unitypackage (28.7 KB)

2 Likes