I'm making a FPS game, and i am having some difficulty making a box or chest which is highlighted when the player looks at it.
I am using raycast from the camera to detect if the player is looking at the box. Highlighting the box (Using a different shader) is easy. but how do i detect that the camera is no longer looking at the box, and the switch back to the original shader?
heres my script:
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var Hit : RaycastHit;
var shader1 = Shader.Find( "Diffuse" );
var shader2 = Shader.Find( "Self-Illumin/Diffuse" );
if (Physics.Raycast (transform.position, fwd, Hit, 100)) {
if (Hit.collider.gameObject.tag=="Highlightable")
Hit.collider.gameObject.renderer.material.shader = shader2;
}
// and then i am lost...
}
thought that maybe there might be a possibility for the script to be on the box itself and then detect whether or not the ray is pointing at it, but i cant seem to find such a code in the scripting reference.
Thx in advance - Sigvard.