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.
chestHighlight.js (attach to all objects getting highlighted)
public var isHighlighted:boolean = false;
var unlitShader:Shader;
var litShader:Shader;
function Update()
{
if(!isHighlighted)
{
renderer.material.shader = unlitShader;
}else{
renderer.material.shader = litShader;
}
}
function LateUpdate()
{
isHighlighted = false;
}
checkHighlight.js (attach to camera)
function Update ()
{
var fwd = transform.TransformDirection (Vector3.forward);
var Hit : RaycastHit;
if (Physics.Raycast (transform.position, fwd, Hit, 100))
{
if (Hit.collider.gameObject.tag=="Highlightable")
{
hit.chestHighlight.isHighlighted = true;
}
}
}
That should about do it for you. I didn’t actually test it, but it’s a fairly simple script (the only part I’m not sure about is hit.chestHighlight.isHighlighted = true, which if it doesn’t work, you can use hit.GetComponent(“chestHighlight”).isHighlighted, but that’s not as efficient.)
There was a couple of problems with your code but i helped alot.
i got i working by using the following to scripts:
HighlightScript.js (On the box that i want highlighted)
public var Highlighted = false;
function Awake (){
gameObject.tag="Highlightable";
}
function Update () {
var shader1 = Shader.Find( "Diffuse" );
var shader2 = Shader.Find( "Self-Illumin/Diffuse" );
if (Highlighted==true)
renderer.material.shader = shader2;
else
renderer.material.shader = shader1;
}
function LateUpdate()
{
Highlighted = false;
}
CamCastScript.js (On the main camera)
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var Hit : RaycastHit;
if (Physics.Raycast (transform.position, fwd, Hit, 100)) {
if (Hit.collider.gameObject.tag=="Highlightable"){
Hit.collider.gameObject.GetComponent("HighlightScript").Highlighted = true;
}
}
}
Well, if you just copied and pasted your code to here, then this line:
Hit.collider.gameObject.GetComponent(“HighlightScr ipt”).Highlighted = true;
has a space in the getcomponent string. It should be:
Hit.collider.gameObject.GetComponent(“HighlightScript”).Highlighted = true;
Also, to be honest, all of the stuff for setting the shader should be in lateupdate, as it would depend on when you added what script, or what the game object is named, or however unity orders the update, so highlightScript.js:
public var Highlighted = false;
var shader1;
var shader2;
function Awake (){
gameObject.tag="Highlightable";
shader1 = Shader.Find( "Diffuse" ); //Moved the setting of these to only the awake to save on processing.
shader2 = Shader.Find( "Reflective/Specular" ); //in the update loop, it has to reassign these shaders every loop. Not very efficient.
}
function LateUpdate()
{
if (Highlighted)
{
if(renderer.material.shader != shader2)
{renderer.material.shader = shader2;
Debug.Log("Highlighted");}
}
else
{
if(renderer.material.shader != shader1){//added a check to try and optimize the code. I think an if comparison is faster then setting a shader to an object.
renderer.material.shader = shader1;
Debug.Log("Not Highlighted");}
}
Highlighted = false;
}
More then likely Unity is running the update loop for this script in your project before it runs the camcastscript.js. This would make it set the boolean to true, but after it’s already assigned the shader. Always look through your code again to see if there are ways to optimize it also. For instance, I added an if statement to check to see if the shader is already set to the shader we want or not. If not, then it sets it. I’m fairly certain it’s easier to compare then it is to set a variable on it’s own, so I added it. (If you had pro, you could actually see the difference in time and it would tell you for sure though by using profiler.)