Unity3D : Underwater Effect

Using unity3d is easy to create awesome terrain with first person camera. We can create an island scene with it’s sea, river, tree, and other objects. We could get a nice sun effect with built in directional light  and flare assets, we could great sky with adding Skybox to our game, even we could get near-far ‘feel’ by enabled the fogs.

But there something missing, the camera render normally when we underwater, there is no different between underwater view and above water view. This feel is necessary when we create scene that have more water than land, or the player can go swimming or diving in our scene. so we need underwater effect script to complete our scene. for the demo you can see here

create new c-sharp script and name it Underwater, and copy code bellow.

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;

			// 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 = false;
		}
	}
}

attach this script to our ‘Main Camera’, if we use built in first person controller, our Main Camera located as child game object of First Person Controller. And also add new component Image Effect > Blur Effect to our main camera, if you can’t find this, import Image Effect (Pro) assets first from Assets > Import Package > Image Effect (Pro Only).

Note that image effect is available on Unity3D  PRO ONLY. If you only has Indie version of unity3D, you should remove/commented this line bellow from Underwater script we has

// 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;

See our underwater sample here

9 Opinion to this post

  1. Camilo Echavarría says:

    Nice tutorial!!

    but this script only works with First Person Controller?, I’ll tried with 3er person controller but it doesn’t work.

  2. Henry says:

    I read a lot of interesting articles here. Probably you spend a lot of time writing, i know how to save you a
    lot of time, there is an online tool that creates high quality, google
    friendly articles in minutes, just type in google
    – laranitas free content source

  3. Alberto says:

    I read a lot of interesting posts here. Probably you spend a lot of time writing, i know how to save you
    a lot of work, there is an online tool that creates readable, SEO friendly posts in seconds, just type in google – laranitas free content
    source

  4. […] d’intégrer du multithreading dans Unity. -Une liste de projets open-source pour Unity. -Un script pour créer un effet sous-marin. -Tudee est un Tile Map Editor gratuit qui exporte en xml. -Best […]

  5. BrumTander says:

    Thanks for this! Now I’m very much a beginner, so I copied the script and I have water fog through my whole level. I have one area with my waterplanes in it, where in the code can I either set the height of the effect so its below my water planes or do i have to have coliders/triggers on the waterplane itself to achieve this? haha as I said, I’m more on the art side and am new to the world of code/unity so I am the truest beginner. Thanks so much!

  6. sadicus says:

    Unity3D : Underwater Effect
    cool tutorial! 🙂
    Unity Pro 4.x How to change “BlurEffect” to use Blur (Optimized), or any other script?
    I would like to control the blur based on distance.

  7. Nivanda says:

    Dragoctis : Depends actually, there are a lot of ways to view a foesrt, angles and such, you would have to specify me i which way, besides the things you want on it, like a river or so so, or rocks or a cliff etc.I could if you specify or give me a reference of your vision ;D

  8. Terry Morgan says:

    It works for me, thanks

    • BrumTander says:

      Thanks for this! Now I’m very much a beginner, so I copied the script and I have water fog through my whole level. I have one area with my waterplanes in it, where in the code can I either set the height of the effect so its below my water planes or do i have to have coliders/triggers on the waterplane itself to achieve this? haha as I said, I’m more on the art side and am new to the world of code/unity so I am the truest beginner. Thanks so much!

Leave a Reply to Camilo Echavarría Cancel reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.