How do I create water physics in Unity?
Answer by runevision
Sep 24, 2010 at 10:04 AM
First of all what is meant by "water physics"? Water physics is not one specific thing, water can be simulated in many different ways.
Still water (in a container)
Relatively still water contained in a container - for example a swimming pool - is simulated in many games, for example Half Life 2. It simulates light objects like wood floating up to the surface, while heavy objects may sink slowly to the bottom etc. There may also be waves on the surface.
This type of water physics can be simulated in Unity without problems (2.x or 3.x, it doesn't matter), it just requires some scripting with AddForce() and such. Basically, whenever an object is below the water surface, add an upwards force corresponding to the buoyancy of the object. When the player or other objects collide with a floating object, be sure to make the impact use AddForceAtPosition() rather than AddForce() in order to get a realistic floating feel, where object may tip over.
As for adding waves, waves can either use a predefined pattern based on a texture, or sinus functions, or waves can be simulated to reach realistically to disturbances in the water. See this post for the latter.
Another way to simulate water is with a particle system. This can be done in many ways, but one major difference is whether there is any inter-particle influence or not, i.e. do particles have any effect on other particles.
Particle system without inter-particle influence
Without inter-particle influence the water can look quite good while being sprayed, for example from a hose or fountain, but the water will never fill up a volume since the particles will just end up occupying the same space rather than spreading out. It can still work quite well for some uses though.
For example, the "gels" in the upcoming Portal 2 are of this type. There there are hoses that spray out gels indefinitely but it never fills up the room; the volume of the gel just "disappear" when touching a surface, and only leaves an infinitely thin "puddle" that doesn't grow bigger even though more gel is added.
This type of water can be created in Unity without problems. For the particle simulation, the standard particle systems in Unity can be used. If a proper water surface is wanted instead of billboards, look into using meta balls.
Particle system with inter-particle influence
In order to simulate the volume that water occupies, the particles of a particle system need to influence each other. This is generally more advanced and much more processing intensive. It can create spectacular water effects, but it is not really ready for real-time applications yet because it requires so heavy calculations. Some tech demos may boast to have real-time water of this kind, but for real-world use cases it's still out of reach.
Answer by duck
Sep 24, 2010 at 10:20 AM
Another area not mentioned so far is water physics for wheeled vehicles (i.e. driving through water).
A simple water trick with wheeled-vehicle-based games is to simply increase the drag of the vehicle's rigidbody based on the depth of any wheels which are below water. To add to this, you can adjust the volume and pitch of a water sound effect loop to the same value (adjust the pitch downwards and the volume upwards as the depth increases). And of course to finish the effect, you could add some visual effects like a particle system at each wheel to show a splashing effect.
This can make driving through water feel very realistic, as it seems to pull on the car and give the thick feel of driving in water.
You can see (and hear) this technique in action here: (Offroad Vehicle "Jackal" Training game)
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
No one has followed this question yet.
Can I detect collision without preventing movement?
Dynamic water Effects
C# Water Rotation
Minecraft - Water Physics
Flowing water with a fixed volume