Breathing underwater

//This script enables underwater effects. Attach to main camera.

//The scene's default fog settings

private var defaultFog = RenderSettings.fog;

private var defaultFogColor = RenderSettings.fogColor;

private var defaultFogDensity = RenderSettings.fogDensity;

private var defaultSkybox = RenderSettings.skybox;

var noSkybox : Material;

var FogDensity = RenderSettings.fogDensity;

function Start () {

//Set the background color
camera.backgroundColor = Color (0, 0.4, 0.7, 1);

}

function Awake () {

GetComponent("GlowEffect").enabled = false;

}

function Update () {

if (transform.position.y < 9.125) {
    RenderSettings.fog = true;
    RenderSettings.fogColor = Color (0.3, 0.32, 0.22, 0.9);
    RenderSettings.fogDensity = FogDensity;
    RenderSettings.skybox = noSkybox;
    GetComponent("MotionBlur").enabled = true;
    GetComponent("AudioSource").enabled = true;
    GetComponent("BlurEffect").enabled = true;
}

else {
    RenderSettings.fog = defaultFog;
    RenderSettings.fogColor = defaultFogColor;
    RenderSettings.fogDensity = defaultFogDensity;
    RenderSettings.skybox = defaultSkybox;
    GetComponent("MotionBlur").enabled = false;
    GetComponent("AudioSource").enabled = false;
    GetComponent("BlurEffect").enabled = false;
}

}

This is my underwater effects control for an RPG/FPS

I want to add a breath timer to this, can anyone help i'm not too good with guis and timers? i presume thats how its done...

I would suggest adding a variable that tells the script if it is already underwater, as well as one for how long we can stay down, and one for when we last took a breath. If we are "underwater" we check to see if we were before now, and either check for drowning, or set the underwater settings, including saving that this was the last time we drew breath. When we surface again, we are no longer "underwater". breathTime is obviously up to you, and I'm not exactly sure what you want to do if we are drowning, so I just said we should `Drown();`

private var underwater = false;
var breathTime = 10.0;
private var lastBreath = 0.0;
//...
function Update(){
    if(transform.position.y < 9.125){
        if(underwater){
            //we were already underwater, check for drowning
            if(Time.time > lastBreath + breathTime){
            //we have been underwater longer than our breath time
            Drown();//or whatever
            }
        }
        else{
            //we were not underwater
            underwater=true;
            lastBreath=Time.time;
            RenderSettings.fog = true;
            RenderSettings.fogColor = Color (0.3, 0.32, 0.22, 0.9);
            RenderSettings.fogDensity = FogDensity;
            RenderSettings.skybox = noSkybox;
            GetComponent("MotionBlur").enabled = true;
            GetComponent("AudioSource").enabled = true;
            GetComponent("BlurEffect").enabled = true;
        }
    }
    else{
        //we are above the height cutoff
        if(underwater){
            //we just came up
            underwater=false;
            RenderSettings.fog = defaultFog;
            RenderSettings.fogColor = defaultFogColor;
            RenderSettings.fogDensity = defaultFogDensity;
            RenderSettings.skybox = defaultSkybox;
            GetComponent("MotionBlur").enabled = false;
            GetComponent("AudioSource").enabled = false;
            GetComponent("BlurEffect").enabled = false;
        }
    }
}