- Home /
Raycast layers
Hello, I need to make an interaction script. So far it looks like this:
void Update()
{
_Fwd = _Camera.transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, _Fwd, out _Hit, _InteractLenght, _InteractLayer))
{
if (_Hit.collider.CompareTag("Door") || _Hit.collider.CompareTag("Drawer"))
{
_Crosshair_Interact();
if(Input.GetKeyDown(KeyCode.E))
{
//_Hit.collider.GetComponent<ToggleAnimation>().Toggle();
}
}
}
else if (_InteractIconToggle)
{
_InteractIconToggle = false;
Crosshair_Idle();
}
}
However, when I use it for interaction layer it doesn't count the default layer which means my raycast can go through walls and interact with objects through the wall. Would I have to do a second raycast using a default layer and returning a true if the object is hit in default layer and interact layer? Or is there any more of an efficient way of doing this?
Thank you for your time.
Answer by khooroko · Aug 03, 2018 at 01:19 AM
Physics.Raycast(transform.position, _Fwd, out _Hit, _InteractLenght, _InteractLayer | 0)
where 0 is the default layer. See https://answers.unity.com/questions/8715/how-do-i-use-layermasks.html for more detailed answers.
if (Physics.Raycast(transform.position, _Fwd, out _Hit, _InteractLenght, _InteractLayer) && Physics.Raycast(transform.position, _Fwd, out _Hit, _InteractLenght, 0))
should do it
Your answer
![](https://koobas.hobune.stream/wayback/20220612172107im_/https://answers.unity.com/themes/thub/images/avi.jpg)