Hello all,
I have enabled fog in the Render Settings of my scene and i'm looking for a way to control the density of the fog within a specified range (to give the impression that an area is 'foggier' than others).
This post in the Unity forums suggests either manipulating the `RenderSettings.fogDensity` property (which i'm trying to do), or using large particle effects (I'll try that if all else fails).
I originally used IF statements to alter the value with time if the player was within a given radius - it worked but, of course, the fog density still increases/decreases if the player is standing still. I want to increase/decrease the density in line with the players approach to a location.
In the FPS tutorial here I found a similar example used for damage and hitpoints:
// The hit points we apply fall decrease with distance from the hit point
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
I tried to modify this for my own uses and below is the script I have so far. It works but I would like to be able to control the maximum fog density (1.0 is too much) and the rate with which the fog density changes - I mean the value subtracted/added to the current density each time (If this is even possible considering i'm tied to player movement).
// Enable fog
RenderSettings.fog = true;
// variable to control max fog density (not used at the moment)
var maxFogDensity : float = 0.1;
// Initial density setting (overidden by code below)
RenderSettings.fogDensity = maxFogDensity;
// variable to control fog range
var fogRange : int = 10;
// variable to control fog speed (amount subtracted/added each time - not used)
var fogSpeed : float = 0.1;
// co-ordinates for 'centre' of foggy area
var fogSpawn : Transform;
function Update () {
var dist = Vector3.Distance(fogSpawn.position, transform.position);
// Increase/decrease the density of the fog according to distance
// Mathf.Clamp01 is a maths function which clamps a value to a position
// no less than 0 and no more than 1
var currentFogDensity = 1.0 - Mathf.Clamp01(dist / fogRange);
RenderSettings.fogDensity = currentFogDensity;
}
I've been playing around with those two lines and my variables for some time now, but i'm just confusing myself now to be honest. I'd be grateful if anyone could shed some light on this.
Thank you.