enter image description here

As you see in picture, i cast rays to (vector3.right * direction) from center of boxCollider's bounds. It can detect the wall mostly, but sometimes it can't detect the wall and pass through it. the layer of wall is 'obstacle' and ray only detect that layerMask. why this happend?

            moveAmount.x = input.x * speed;
            moveAmount.z = input.z * speed;

            directionX = Mathf.Sign(moveAmount.x);
            rayLength = Mathf.Abs(moveAmount.x) + .015f;

            if (Physics.Raycast(rayOrigin, Vector3.right * directionX, out raycastHit, rayLength, collisionMask))
  • \$\begingroup\$ First debugging steps: does anything change if you omit the rayLength and collisionMask arguments? \$\endgroup\$
    – DMGregory
    Jun 13, 2020 at 2:54
  • \$\begingroup\$ yes. i think adjust raylength is sole solution. thanks. \$\endgroup\$
    – Hree
    Jun 13, 2020 at 8:32

1 Answer 1


In this case, adjust rayLength will be a solution. if rayLength is too short, it will raycast inside of wall at next frame.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .