Hi I have this script:
var isHovering : boolean;
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray.origin, ray.direction, hit, 100))
{
if(hit.collider.tag == "Item")
{
isHovering = true;
}else if(hit.collider.tag == "Item"){
isHovering = false;
}
}
but when ever the script runs it flickers between true and false? What is the problem and how do I fix it?
Thanks
Your code should be more like this:
var isHovering : boolean;
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray.origin, ray.direction, hit, 100))
{
// we hit something.. check the tag of the hit object
if(hit.collider.tag == "Item")
{
// we hit our tag
isHovering = true;
}
else
{
// we didnt hit our tag.. we hit something else
isHovering = false;
}
}
else
{
// we didnt hit a thing
isHovering = false;
}