0
\$\begingroup\$

I have the following simple raycast from a capsule on a cube:

bool hit = Physics.Raycast
(transform.position, Vector3.down,
jumpSettings.groundJumpDistanceCheck, 
jumpSettings.layerMask);

print(hit);

enter image description here

When my layerMask is 8 (terrain), I have set my layer on my cube and my groundJumpDistanceCheck is 2. Then my hit is false.

When my groundJumpDistanceCheck is set to Infinity. Then my hit is still false.

However when I remove the distance parameter:

bool hit = Physics.Raycast
(transform.position, Vector3.down,
/* jumpSettings.groundJumpDistanceCheck, */ 
jumpSettings.layerMask);

print(hit);

Then hit returns true. This is odd for me, as far as I know when you exclude the distance parameter it's supposed to be Infinity by default. But when I explicitly give Infinity as the distance, it returns false.

I have also used the following code in an attempt to debug my issue, using the desired distance 2f

Debug.DrawRay(transform.position,
Vector3.down * jumpSettings.groundJumpDistanceCheck, Color.red, 60f);

enter image description here

As you can see, the ray does intercept the cube.

I need the reycast to return true with a distance of 2f.

\$\endgroup\$
  • \$\begingroup\$ Don't forget to show where you populate your jumpSettings.layerMask field - either in the inspector or in code. A common error is to use the layer index as a layer mask, instead of setting the corresponding bit in the mask bitfield, so showing where you provide the value can eliminate this case. \$\endgroup\$ – DMGregory Jul 12 '17 at 0:22
1
\$\begingroup\$

So after doing some more research. I realized that the layermask param was excluding layers. This is what it should be:

bool hit = Physics.Raycast
(transform.position, Vector3.down,
jumpSettings.groundJumpDistanceCheck, 
1 << jumpSettings.layerMask);

print(hit);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.