- Home /
Question by
Arisstephenson · Mar 22, 2018 at 11:41 PM ·
raycastlayermask
Layermask doesn't ignore layers
I am trying to make a Raycast only collide with objects on the "template" layer, but no matter what I try the ray detects collisions on the default layer too.
void RenderTemplate() //Renders building template
{
int mask = (1 << LayerMask.NameToLayer("template"));
RaycastHit templatehit;
Physics.Raycast(MouseRay, out templatehit, mask);
template.SetActive(true);
template.transform.position = Vector3.ProjectOnPlane(templatehit.point, Vector3.up) - Vector3.up * 1.5f;
}
Why doesn't this work?
Comment
Best Answer
Answer by Cornelis-de-Jager · Mar 22, 2018 at 11:53 PM
Easy Solution:
Physics.Raycast(MouseRay, out templatehit, Mathf.Infinity, mask);
The reason this happens is because the function you are trying to use is:
Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers)
Remember the mask is an Integer, meaning it can be used as the maxDistance variable. Since the maxDistance variable is infact before the mask layer, C# interperates it as the max distance instead of the Mask Layer.