Unity3D: Underwater Effect
Make submerged scenes feel underwater in Unity3D: swap fog color and density below the water line, and add an optional blur image effect.
Creating immersive water scenes in Unity3D requires more than just terrain and objects. While the engine excels at building landscapes with first-person cameras, adding skyboxes, directional lighting, and fog effects, something essential is missing: realistic underwater rendering.
When the camera is underwater, there is no visual distinction between submerged and surface views. This limitation is particularly noticeable in water-heavy scenes or games featuring swimming and diving mechanics. The solution involves an underwater effect script to enhance visual immersion.
Implementation
Create a new C# script named Underwater:
using UnityEngine;
using System.Collections;
public class Underwater : MonoBehaviour {
// Attach this script to main camera
// And Define variable underwaterLevel up
// to your water level or sea level Y-coordinate
public float underwaterLevel = 7;
// These variable to store
// The scene default fog settings
private bool defaultFog = true;
private Color defaultFogColor;
private float defaultFogDensity;
private Material defaultSkybox;
private float defaultStartDistance;
void Start () {
// store default fog setting
// we need to restore fog setting
// after we go to surface again
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
defaultStartDistance = RenderSettings.fogStartDistance;
// set the background color
camera.backgroundColor = new Color(0, 0.4f, 0.7f, 1);
}
void Update () {
// check if we below the sea or water level
if (transform.position.y < underwaterLevel) {
// render new fog with blue color
// Or you can change the color to
// match your water
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.1f;
RenderSettings.fogStartDistance = 0.0f;
// add this if you want to add blur effect to your underwater
// but first add Image Effect (Pro) Package to your project
// Add component Image Effect > Blur to Main camera
this.GetComponent<BlurEffect>().enabled = true;
} else {
// revert back to default setting
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
RenderSettings.skybox = defaultSkybox;
RenderSettings.fogStartDistance = defaultStartDistance;
this.GetComponent<BlurEffect>().enabled = false;
}
}
}
Setup Instructions
- Attach the script to the Main Camera (typically a child object of First Person Controller)
- Add the Image Effect > Blur Effect component to the main camera
- Import Image Effect (Pro Only) assets via Assets > Import Package > Image Effect
Important Notes
The blur effect requires Unity3D Pro. For Indie versions, comment out or remove these lines:
// remove or comment this code if you use Unity3d indie version
this.GetComponent<BlurEffect>().enabled = true;
// and this code also
this.GetComponent<BlurEffect>().enabled = false;