Fog and Render Settings - Question

Hello guys,

I’ve run into a problem and am hoping that some of you guys might be able to help w/ Unity 4.6.3f.

The question revolves around this: I found a tut on Youtube about adding a fog effect in the water and it worked fine. However, it used a script on the “Main Camera” and had waterLevel variable meaning it was unusable for water at different heights and/or lows. So, I decided to try and make the a new JavaScript function and make the code work of a isTrigger collider. It did work fine on the “Dam” object. Then I tried using basically the same code with minor differences for checking purposes to work on a higher level, a “River” object and I couldn’t get it to work at all. Playing around with the objects I found I could have on one or the other but not both.

So, I am hoping that you make be able to tell me what changes I would need to make to the code to get it working on both objects.

Thankyou so much for your time.

The JS code is below:

#pragma strict

//not needed here - should remove
var callObject: GameObject;

//set water level - not used anymore
private var waterLevel: float;

//use a boolean value to stipulate whether the controller is underWater
private var isUnder: boolean;

// have colours/vars to change appearance when in water
//basically just changing the fog appearance
//this sets fog to around about normal
var normColour = new Color (0.5f, 0.5f, 0.5f, 0.5f);

//this sets fog much deeper
//var underDamColour = new Color (0.99f, 0.01f, 0.01f, 0.01f); //deep red veery cool :)
private var underDamColour = new Color (0.22f, 0.65f, 0.77f, 0.5f);




function OnTriggerEnter(object : Collider){
    //Quick and nasty
    print("Dam Collider entered ...........");
    isUnder = true;
    //callObject.SendMessage("callFunction");
    }


function OnTriggerExit(object : Collider){

    //Quick and nasty
    print("Dam collider exited ...........");
    isUnder=false;
    RenderSettings.fog = false;
    }



function callFunction(){
    print("function called............................................");
    //no longer used
    }//-->End callFunction



function FixedUpdate(){
    //print("FixedUpdate..");
    //call function according to isUnder value
    if(isUnder) SetUnderDam();
    if(!isUnder) SetNorm();

    }//-->end FixedUpdate()



//changer Render Setting for on land :)
function SetNorm(){
    RenderSettings.fogColor        = normColour;
    RenderSettings.fogDensity     = 0.002f;

    //stop the caustic generator from making those weird caustics on the land
    GameObject.Find("Caustics Projector").GetComponent(Projector).enabled = false;
    }//-->end SetUnder()



//change Render Setting for underwater
function SetUnderDam(){
    //print("Under");

    RenderSettings.fogColor = underDamColour;

    //far to thick to see anything
    //RenderSettings.fogDensity     = 0.99f;
    RenderSettings.fogDensity     = 0.05f;
    RenderSettings.fog = true;

    //make the weird caustics appear in the water
    GameObject.Find("Caustics Projector").GetComponent(Projector).enabled = true;

    }//-->end SetUnder()

Hi, I’m not sure if you understand this, but fog is a global parameter for the scene. When you enable the fog, it is shown everywhere in the scene and only “one” fog can be shown at the same time. You can also play with fog settings in the editor instead of the script. Unity - Manual: Render Settings maybe it will become clearer.

Now to help with your code, I’m not sure how your triggers are set up. If your triggers overlap, then OnTriggerExit of the first trigger may disable the fog set by the second trigger. I’d suggest to do the following. Move your FixedUpdate, SetNorm, SetUnderDam functions to a new script. To the same new script add bool variables for “isUnderDam”, “isUnderRiver” and so on. Then in the FixedUpdate instead of checking “if (isUnder)”, you would check for “if (isUnderDam) {} else if (isUnderRiver) {}” and set the fog values appropriately. Leave only the OnTriggerEnter and OnTriggerExit functions in scripts that are attached to your triggers. In these functions set the appropriate values for the “isUnderDam”, “isUnderRiver” variables of that new script. This should work even when triggers overlap.

Finally thinking about the performance, there is no need to set the same fog values on each FixedUpdate call. You can set it once when it changes and that’s it. You can do that by calling your SetUnderDam, SetNorm functions directly from OnTriggerEnter and OnTriggerExit. In the SetNorm function you would check “if (!isUnderDam && !isUnderRiver)” and only then you would reset the fog value to the “normal”. I hope it helps.

1 Like

Hello JuliusM,

Just for the record the triggers were not overlapped I made sure that wouldn’t happen just to stop one major headache from occurring ;).

Your reply was highly detailed, some of it was waay above my pay-grade :p. Way over this noobs head, but, took the little bit that I could understand about taking out the FixedUpdate call - for performance reasons - and running directly into the script for the fog. From that tutorial I found on the web I was lead to believe that was FixedUpdate/Update function was necessary when anything was being drawn to the screen. Anyway, doing that enabled the script to run properly and can generate the fog from either the River or Dam triggers, plus starting the caustics projector for the dam.

Thanks so much for your reply.

Problem: solved!