i have a working method for making objects fade if they appear between camera and target. idea was to cast a ray and if intersects something along the way to the target it makes it fade out. the problem with this method was that i needed to cast many rays to fade out all objects properly. i decided to do it the other way, somebody proposed some time ago.
idea is to make a cylinder that will start at the camera position and end at the target position, everything that collides with the cylinder is being faded out.
so i made a cylinder, it automatically gets capsule collider. i made isTrigger to true, i added also rigidbody to it ( i dont know if it is needed) set isKinematic to true. the other objects have colliders (mesh colliders) and i set isTrigger to true, for them.
the code i used is this:
function Awake(){
transform.renderer.enabled=false;
}
function OnTriggerEnter(col : Collider){
SmoothFade(col);
Debug.Log(col.name);
}
function OnTriggerExit(col : Collider){
SmoothUnfade(col);
}
function SmoothFade(col : Collider){
var t = 0.0;
while (t<1.0){
t+=Time.deltaTime *(1.0/0.5);
col.renderer.material.color.a=Mathf.Lerp(1.0,0.1,t);
yield;
}
}
function SmoothUnfade(col : Collider){
var t = 0.0;
while (t<1.0){
t+=Time.deltaTime *(1.0/0.5);
col.renderer.material.color.a=Mathf.Lerp(0.1,1.0,t);
yield;
}
}
when i start the game, and select the cylinder in the editor window and drag it around the scene, objects collider, and Debug.Log show what object is collided with cylinder, but it doesnt fade away…why? what am i doing wrong?
also i have no idea how to position cylinder, one part at the target center and other at the camera position?