- Home /
Raycast layer mask problem
Hi all, I am somewhat new to unity but think I have a good understanding of how things work.
My basic problem is that I want to use a layer mask with
Camera.main.ScreenPointRoRay
I am using a Physics.RayCast
from my camera to my player to detect if anything is in the way, if it is then I am hiding it so we can see the player through it using:
camHit.transform.renderer.enabled = false;
This works fine, but once it's hidden I want to use a layermask so my click to move function works properly (this is what uses the Camera.main.ScreenPointRoRay
), otherwise my ray hits the object in the ways collider.
If I disable the object I hit collider my Physics.Raycast hits the player and so hides the object again, then unhides, and loops forever.
I may have gone about doing this in the completely wrong manner, hopefully someone can assist.
Here is my function for detecting if objects are in the way.
function hideObjects()
{
var camHit : RaycastHit;
var fwd = Camera.main.transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(Camera.main.transform.position, fwd, camHit, 50))
{
print(camHit.transform.tag);
if(camHit.transform.tag == "roof")
{
theRoof = camHit.transform;
camHit.transform.renderer.enabled = false;
camHit.transform.collider.enabled = false;
}
else if (camHit.transform.tag == "player")
{
theRoof.renderer.enabled = true;
theRoof.transform.collider.enabled = true;
}
}
Debug.DrawLine (Camera.main.transform.position, camHit.point, Color.yellow, 0.25);
}
and here is a snippet of my click to move function. This is the Ray that will hit the building collider, this is the function I want to put a layer mask on but I don't know how judging by the options on this page:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//MOVEMENT CODE HERE
}
Thanks in advance :)
Mike
Answer by AlucardJay · Apr 19, 2013 at 04:16 PM
Check my answers here :
Yes!
It's now working. I was trying to do my layer mask in the wrong place. Your posts showed me the way to go!
Thank you very much. I will name a character in my game after you...