Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Created September 27, 2013 18:00
Show Gist options
  • Save keiranlovett/6732518 to your computer and use it in GitHub Desktop.
Save keiranlovett/6732518 to your computer and use it in GitHub Desktop.
Attach to your main camera and key in the y-position of your water plane for "underwaterLevel". Leave "noSkybox" blank.
using UnityEngine;
using System.Collections;
public class Underwater : MonoBehaviour {
//This script enables underwater effects. Attach to main camera.
//Define variable
public int underwaterLevel = 7;
//The scene's default fog settings
private bool defaultFog = RenderSettings.fog;
private Color defaultFogColor = RenderSettings.fogColor;
private float defaultFogDensity = RenderSettings.fogDensity;
private Material defaultSkybox = RenderSettings.skybox;
private Material noSkybox;
void Start () {
//Set the background color
camera.backgroundColor = new Color(0, 0.4f, 0.7f, 1);
}
void Update () {
if (transform.position.y < underwaterLevel)
{
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.04f;
RenderSettings.skybox = noSkybox;
}
else
{
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
RenderSettings.skybox = defaultSkybox;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment