Why won't Physics.RaycastNonAlloc mask layers properly?
I solved this and am just posting it here for anyone else who has the same trouble.
Unity 2019.2 was refusing to mask layers when I used Physics.RaycastNonAlloc
, despite setting the ray, the RaycastHit[], and the layer int correctly, like this:
var hits = Physics.RaycastNonAlloc(ray, _physicsHitObjects, _layerMask);
It turned out what I was doing wrong was that, although the editor gave no warning, there is no RaycastNonAlloc expression which allows only these three parameters. The shortest layer masking version is:
Physics.RaycastNonAlloc([ray or Vector3D], RaycastHit[], float maxDistance, int layerMask)
Unity was mistaking my layerMask int for a maxDistance float, since the float has to come first. The quick, easy fix to the above code was simply:
var hits = Physics.RaycastNonAlloc(ray, _physicsHitObjects, 100f, _layerMask);
Anyway, no need to respond, but I hope this helps someone.
...possibly my future self when I make the mistake again.
Answer by Abercromby3 · Feb 12, 2020 at 06:55 PM
I solved this and am just posting it here for anyone else who has the same trouble. Unity 2019.2 was refusing to mask layers when I used Physics.RaycastNonAlloc, despite setting the ray, the RaycastHit[], and the layer int correctly, like this: var hits = Physics.RaycastNonAlloc(ray, _physicsHitObjects, _layerMask); It turned out what I was doing wrong was that, although the editor gave no warning, there is no RaycastNonAlloc expression which allows only these three parameters. The shortest layer masking version is: Physics.RaycastNonAlloc([ray or Vector3D], RaycastHit[], float maxDistance, int layerMask) Unity was mistaking my layerMask int for a maxDistance float, since the float has to come first. The quick, easy fix to the above code was simply: var hits = Physics.RaycastNonAlloc(ray, _physicsHitObjects, 100f, _layerMask); Anyway, no need to respond, but I hope this helps someone. ...possibly my future self when I make the mistake again.