Object fading between player and camera! please help!

hello!
ive taken some info from the unity documentation and ive got most of it working, here is the code:

function Update () {
var hit: RaycastHit;
var ray: Ray = GetComponent.().ViewportPointToRay(new Vector3(0.5, 0.5, 0));

if(Physics.Raycast(ray, hit)){
var objectHit: Transform = hit.transform;

if (objectHit.tag == “HideObject”){
objectHit.GetComponent.().material.shader = Shader.Find(“Transparent/Diffuse”);
objectHit.GetComponent.().material.color.a = 0.2;
}

}
}

it works when i walk behind an object, but it doesnt go back to fully visible when i go away from the object.
is there a simple solution for this, preferably in javascript(im using an older version of unity)? Any help would be appreciated! Thanx.

The main thing is if the object is NOT being hit by the raycast, the alpha needs to be set back to 1.0.

In order to do this, you need to be able to remember what object was hit so you can restore its alpha on the next frame update.

Easiest way to do this is to move “var objectHit: Transform” outside of Update() and into the class scope.

Then in Update(), check if objectHit is not null, and if it’s not null, set its alpha to 1 (and restore the shader to what it originally was).

Thank you blackpete! Ill check it out tommorrow!

Since I assume you’re not too far along, it is highly suggested to stop using UnityScript and use C# instead.
The language you are using is being deprecated:

On the website: click on the “C#” button to view the example codes in C#


.

@BlackPete Hi again! I tried to do what you told me to, but i couldnt get it to work, could you please provide the code for it? i really dont understand how im supposed to do this.

@BlackPete Nvm i figured it out! thank you anyway for your explanation. :smile: