I am using raycast to check for touches on object, however the problem I have is that the raycast is passing thru walls and activating the touch code on objects on the other side of the wall. For example on one side of the wall there is a torch and on the other is a chest. WWhen player touches the torch the chest on the other side opens.
Is there a way to stop raycasts from passing thru certain objects?
On Torch objects the code is
function Update ()
{
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
if (torchLit == false){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider && collider.Raycast (ray, hit, 100.0))
{
if(hit.collider.tag == "Torch"){
OnMouseDown();
}
}
}
}
}
And on my chest object
function Update () {
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider && collider.Raycast (ray, hit, 100.0))
{
if(hit.collider.tag == "chest"){
OnMouseDown();
}
}
}
if (chestOpen == true){
ChestOpened();
}
}
Thanks in advance.