2D Water and air physics

Hello,

I’m building a 8bit style 2D side-scrolling game. The character swims underwater and also flies above the water line.

  1. What are the physics that simulate water so that when the character goes under the water line there is some factor of buoyancy and water resistance?

  2. And how do I change the physics when the character transitions from water to air.

I’m attaching a quick mockup to give you guys an idea of what the scene looks like. 50 / 50 air and water.

I’m very new to Unity. So be gentle :slight_smile:

34546-wate_line.jpg

I just happen to have finished coding a game based on this exact concept, its currently sitting in the art department for finishing. Look for the launch of Pond Wars within the next month.

Here are the basic ideas. Not everything will be applicable, as my game is more interested in objects floating on the water surface, rather then under the water.

  • Define the surface of the water. Pretty simple if your surface is flat. If you want waves you’ll need some more though into this
  • Define what percent of the object is below the surface of the water. I used the bounding boxes on the Collider2D. For more accurate representation I split the collider up into several smaller colliders, this may or may not be required for your application.
  • Add a force upwards (world space) to your RigidBody2D that is proportional to the volume of the GameObject and the percent submerged. Again splitting colliders can help.
  • Set the linear and angular drag proportional to the percent submerged.

There are also some prebuilt solutions on the web.

Let me know if you need more help and I’ll put up some pseudo code.