- Home /
change GameObject color due to distance
Okay so i've managed to create a script that takes the distance of the camera in relation to the gameObject and turns the color on the objects material from white to red. although once the camera reaches 3.5 or lower it just switches, how can i change this script to fade from one colour to another with every unit of distance the camera is. in other words "fade the colour to another when the camera is nearer"
var camR : Transform; var colorStart = Color.white; var colorAlpha = Color.red;
function Update () {
var dist : float = Vector3.Distance(camR.position, transform.position);
var lerp = Time.deltaTime;
if (dist <=3.5){
renderer.material.color = colorAlpha;
}else if (dist >3.5){
renderer.material.color = colorStart;
}
}
Answer by Borgo · Jan 28, 2011 at 12:31 PM
Use the Color.Lerp function.
var camR : Transform; var maxCamDist : float; //max distance of camera var colorStart = Color.white; var colorAlpha = Color.red;
function Update(){ var dist : float = Vector3.Distance(camR.position, transform.position);
renderer.material.color = Color.Lerp(colorStart, colorAlpha, dist/maxCamDist);
}
Another way is to use FOG.