trying to figure out better method for fading objects

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?

You can’t change individual members of a material’s color. You need to set a local variable = to it, change the local variable, and then set the color = to the local variable.

var newColor : Color = col.renderer.material.color;
newColor.a = Mathf.Lerp(0.1,1.0,t);
col.renderer.material.color = newColor;

but Smoothfade works for other method i was using before…i just checked and it works! i just forgot to give transparent material to the objects that i want to fade…i will have to incorporate that into function so that i dont need to worry about that…

one thing left is how to position the cylinder properly to the camera position and target position…?

@GargerathSunman - thanks for looking into this

ok, i here is the link to the original thread:http://forum.unity3d.com/viewtopic.php?p=336043#336043

The capsule collider has a direction setting which aligns it along the local X, Y or Z axis at your choice (you will probably want Z to align it with the camera). The collider also has a property called Center that allows you to specify where the capsule originates from. If the capsule needs to be able to point in any direction, you can place it on a child object of the camera and change its local rotation.