Physics.Raycast ignores LayerMask
Yes, yes, I know there've been multiple questions about the exact same title and topic.
However none of them helped. It's like my Physcis.Raycast behaves in other way. To test my problem I created simple Unity project. There're only 3 objects in it: a Sphere, a Box & a Cylinder. I also added 3 user layers of the same names. As you may suspect, Sphere is on layer Sphere, Box on layer Box, and Cylinder on layer Cylinder.
Then I created this script:
using UnityEngine;
public class HitMe : MonoBehaviour
{
public LayerMask layerMask;
void Update()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool gotHit = Physics.Raycast(ray, out hit, layerMask);
Debug.Log(string.Format("mask == {0}, gotHit == {1}, hit == {2}",
layerMask.value, gotHit, hit.collider));
}
}
... and attached it to my MainCamera. However, regardless of what I set as HitMe.layerMask, I get hits when I hover my mouse over any of these 3 objects (even though I suspect, that mask set to Cylinder should give me only Cylinder, and NULL, when I hover Sphere, or Box, or nothing).
How come?
So you tried 512, 1024, 2048 as values of layer$$anonymous$$ask? How are you deciding your value or do you do it all by name?
Answer by Bennett-Lynch · Apr 17, 2016 at 04:53 PM
So here's your problem. What you think is your layerMask parameter is actually your maxDistance parameter. Based on your arguments, this is the method you're calling:
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance)
LayerMasks can be implicitly converted to ints, which can be implicitly converted to floats.
Change your function call to this and it should work as expected:
bool gotHit = Physics.Raycast( ray, out hit, Mathf.Infinity, layerMask );
If you're using Visual Studio, you can mouseover the function name to see which overloaded function you're actually calling.
Your answer
Follow this Question
Related Questions
raycast going through walls 0 Answers
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer
How to ensure raycasts hit colliders inside of trigger colliders on objects that have a rigidbody? 0 Answers
LayerMask Not working when raycasting to Specific layer. 0 Answers
Why my ray doesn't hit the collider? 1 Answer