PowenKo > Unity > physics > How to create water physics in Unity?

March 6, 2012   ·   0 Comments

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.

Particle systems

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.

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter


Readers Comments (0)

You must be logged in to post a comment.

PowenKo’s Online Store




Recent Posts