- Home /
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.
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?
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 :)
I looked into using this: https://www.assetstore.unity3d.com/en/#!/content/9149
This would be my first time buying something from the asset store. Can I can just pull the code and assign it to my assets?
Any other suggestions?
Thanks
For physical buoyancy you can take a look at this simple you tube video, the script is short and very effective, it will not apply bouyancy when the object is above the water line(a height var which you can define) :
http://www.youtube.com/watch?v=mDtnT5fh7Ek
For water resistance play around with the drag settings of the rigidbody, when in contact with water apply the drag(you can use a simple trigger over the water area for this) and when out of the water return the drag settings to normal.
Answer by Kiwasi · Oct 30, 2014 at 10:46 PM
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.
What platforms are you releasing Pond Wars for? I like seeing what the people here have made :D
Off topic: Its webplayer for at the moment. Plan is $$anonymous$$ongregate first, Facebook if that works.
If there is real interest in the game from players then I will be working on a 'Online' version. That will have multiplayer networking across the webplayer, iPhone/iPad and android.
I'll put a link on my profile page here when its live.
Your answer
Follow this Question
Related Questions
Audio isn't playing for some reason 1 Answer
Swimming/Underwater 3 Answers
(Updated) Unity Pro water works on preview, but not on Mac standalone export. 0 Answers
Is the current basic water bugged? 0 Answers
Water Basic Has no texture 1 Answer