Advertisement
  1. Game Development
  2. Unity 3D
Gamedevelopment

How to Create Realistic Water in Unity

by
Difficulty:BeginnerLength:MediumLanguages:
Final product image
What You'll Be Creating

Unity is a multi-platform game engine developed by Unity Technologies and is used to create video games and applications for several devices such as consoles, mobile devices, computers, and even websites. Unity's core advantages are its robustness, portability, and community; Unity targets several well-known APIs such as Direct3D, OpenGL, OpenGL ES, and the recent Vulkan.

Due to the aforementioned characteristics, Unity has become more and more popular among AAA software development houses and aspiring game programmers.

Unity supports several technologies and components. One of the key components is the Water system. Unity's water system allows you to add different types of water into your application or game, namely Water Basic, Water Pro, and Water4. By default, Unity includes several water prefabs (including the necessary shaders, scripts, and art assets).

This tutorial will focus on explaining how the water in Unity works, and how to use it to create vast and rich terrain environments.

Prerequisites

First, ensure you are using the latest version of Unity; otherwise, you may find small differences following the tutorial and using the physics joints.

To start this tutorial, you have two paths to choose from:

  1. Start it right away using the available Starting Project.
  2. Complete the previous Unity tutorial called Unity Terrain tools.

Either way, we will get you covered, but taking your time and starting in the previous one will give you more insights about Unity and its tools.

Water Basic

I'm assuming that you either started by using the current Starting Project or you have finished the previous Unity Terrain tools tutorial. If that’s not the case, you can start a new project, but you must import the environmental asset package and create your own terrain. If you are using a new project, go to the main menu, Assets > Import Package and select Environment.

From now on, the tutorial is focused on the current Starting Project. By analysing the terrain, you can see that, in the center, the ground is a little bit deeper. That area was created specifically for you to create a lake. One way to do this is using the Water Basic prefab.

WaterBasic - Starting point

In the Project tab, open the Assets folder and then open Environment. Here you will find two water folders: Water and Water (Basic). Initially, you will work with Water (Basic). Open it and then open the Prefabs folder. Two prefabs are available: WaterBasicDaytime and WaterBasicNightime.

WaterBasic - Prefabs

Before moving on, here's a quick explanation of prefabs. A prefab acts as a template of a game object. It allows you to create new object instances in the scene, and any edits made to a prefab asset are immediately reflected in all instances produced from it. 

However, you can also override components and settings for each instance individually. This can be quite helpful if you need to have several objects of the same type in a scene or in several scenes. By using prefabs, you don’t need to create every single object from scratch, and if you want to make an adjustment to it, it will automatically update all instances.

As you may have guessed by the names, you should use WaterBasicDaytime if you have a scene during the day, and you use WaterBasicNightime in nighttime scenes. Both of them work in exactly the same way, but the second one is set to be darker.

In this project you have a daytime scene, so you are going to use the WaterBasicDaytime prefab. Select the prefab with the mouse, and drag it to the scene (more or less into the hole in the ground, similar to the next figure). This will add the WaterBasicDaytime to the scene.

Next, select the water and adjust its Position and Scale.

WaterBasic - Position and shape

When you have the water positioned and configured as you want, let's take a quick look at what you can do with it. If you select it and go to the Inspector tab, you will see several components: a Transform component that you have just used, a Water Basic Plane (Mesh Filter), a Mesh Renderer where the Material for the water is set, a Water Basic Script, and a Shader.

WaterBasic - Inspector Properties

Inside the Mesh Renderer, you can change some options related to shadow generation like Cast Shadows or Receive Shadows. You can also modify several properties related to reflections. Set Cast Shadows and Receive Shadows to On. Next, for Reflection Probes, let’s set it to Blend Probes And Skybox.

WaterBasic - Mesh Renderer

The WaterBasicDaytime Shader lets you set some parameters for the water, like the Wave scale and the Wave speed. You can play with those values to see some graphical changes, but for now leave the default values.

WaterBasic - Water Basic Day Time

Now, in order to see the final result, place the camera in a good position. To do that, move to the Scene view and, once you have the view you want, select the Main Camera game object. Now go to the menu GameObject, and select Align With View. This will place the camera in the position you want. Press play.

WaterBasic - Final Result

From here, you can go back and change the prefab properties to see the differences between properties. Play with Cast Shadows, Wave scale, and Wave speed to sense the modifications.

Water Pro

The next step is to learn how to create and configure a lake using Water Pro. Before moving on, delete the WaterBasicDaytime game object from your scene.

Next, in your Project tab, open the Standard Assets folder and open Environment again. This time, open the Water folder. Inside you have two folders: Water and Water4. In order to use Water Pro, you must open the Water folder. Next, open the Prefabs folder.

Water Pro - Prefabs

Just like with Water Basic, you have two prefabs in the folder: WaterProDaytime and WaterProNightime. Both of them work in exactly the same way, and as aforementioned WaterProDaytime is set for daytime scenes, while WaterProNightime is set for darker environments.

Since you have a daytime scene, you should choose WaterProDaytime. Select the prefab and drag it into the scene. Again, select the WaterProDaytime game object and adjust its Position and Scale.

As soon as the lake gets a good size, you will instantly see differences in the graphical quality of the water compared to Water Basic.

Water Pro - Adjustments

When you have the water in place, please take a quick look at what you can do with it. If you select it and then look at the Inspector tab, you will see several components: a Transform component, a Water Plane Mesh, a Mesh Renderer, a Water (Script), and a Shader.

Water Pro - Inspector

The Mesh Renderer properties are similar to the previous one. Set both Cast Shadows and Receive Shadows to On. Next, change Reflection Probes to Blend Probes And Skybox.

The water Script presents further properties that can be modified. These properties are not available for the previous water type.

Water Pro - Scripting

You can set the Water Mode to Simple, which will make the water work like the basic water, to Reflective, which will make the water have reflection but no refraction, or Refractive, which allows both reflection and refraction. You can Disable Pixel Lights, which will reduce the quality of lighting in the reflection/refraction in order to gain an increase in performance. 

You can set the Texture Size, which refers to the resolution of the generated water texture. The Clip Plane Offset value is the distance below the water that the reflected or refracted image appears. You can set the Reflect Layers affected as well as the Refract Layers. These are layer masks that control which scene objects are visible in the water. By default, all layers are selected.

In the Shader component, you can change the Wave Scale, and change the values of Reflection and Refraction Distort. These will determine the apparent height of the ripples in terms of their effect on reflected or refracted objects.

You can also set the Reflective color of the water to control the tint that will be applied to objects underwater.

Water Pro - Reflection controls

If you press play, you can see your Water Pro in action. This is a way more realistic water than the Water Basic; however, in terms of computational power required, it is also more intensive.

Water Pro - Final Result

Water4

To test Water4, you need to delete the lake you have just created. 

In your Project tab, open the Standard Assets > Environment. Open the Water folder and then the Water4 folder. Next, open the Prefabs folder. Here you find two prefabs: Water4Simple and Water4Advanced.

Water4 - Prefabs

If you select the two prefabs and compare them, you will notice that they have the same components and work in exactly the same way. Then what’s the difference between the two?

Well, you can see that by selecting the water material they use. The advanced version includes additional Shore and Foam textures. These textures are applied on the crests of waves and also where terrains and other objects interrupt the wave mesh, like the shore.

Select the Water4Advanced prefab and drag it into the scene. The next thing to do is to adjust the Position and Scale of the water plane.

Water4 - Adjustments

Note that when you add the prefab, Unity will automatically create a reflection scene game object. This game object allows you to change the water reflection in your scene. The camera component on reflection game object allows the Water4 to reflect your scene.

Water4 - Inspector

Now, if you select the Water4Advanced game object, you will see that you have several properties that you can change.

Water4 - Inspector Scripts

The first field in the Water Base (Script) component allows you to define a material for your water. You can also define the Water Quality as Low, Medium, or High. If the Edge Blend option is enabled, Water4 will blend the edges of the water mesh.

The next component is Specular Lighting. This script adjusts the specular quality of the water. As you can see, it has a reference to specular game object transform. You can use a reference to indicate any transform of a specular light source. This can be, for example, your directional light transform.

Next you have the Planar Reflection component. This one is used for real-time planar reflections. The reflections are calculated in relation to the height of the parent mesh that has this script attached. You can set several parameters as the reflection masks, turn reflect skybox on or off, or define a clear color. Let’s just leave these with the default values.

Finally, you have the Gerstner Displace component, which allows you to control wave generation.

If you press the play button, you can now see the Water4 in action, creating a lake in the center of the terrain.

Water4 - Final Result

Conclusions

This concludes the tutorial about Unity Water. You learned about the three main types of water: Water Basic, Pro, and Water4. With this knowledge, you can now create new rich environments, or modify and improve current ones, for your next cutting-edge game or application.

Unity has an active economy. There are many other products that help you build out your project. The nature of the platform also makes it a great option from which you can improve your skills. Whatever the case, you can see everything we have available in the Envato Marketplace.

If you have further questions or comments, as always, feel free to drop a line in the comments section.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.