Can a Raycast Be Made to Skip a Collider?

Hello,
I’m using a simple FPS controller (capsule, rigidbody, collider, camera, controller). While following a tutorial about using raycasts for picking up objects in the first person the problem came up that sometimes the raycast (which begins on the camera) would hit the FPSController presumably because it was clipping the edge of the collider. The tutorial said they’d get back to fixing that but the tutorial series ended and it was never resolved. Are there any ways I can perhaps exempt the player object so that when the ray is cast it passes through it without returning a hit? The only crude solution I’ve had so far is moving the camera to the front of the FPSController prefab but I suspect this will lead to issues and would rather it be back in the middle-top.
Any advice would be appreciated!

Some options:

  • change layers around temporarily or otherwise
  • use raycastall

Off the top of my head. Note, the raycast all function isn’t guaranteed to be ordered by distance.

Hmm, Thanks! being able to make layers would be pretty handy, I didn’t even know I could do that in Unity. I guess I can just put the player body or the interactable objects on some kind of alternate layer?

Yep that’s the done thing! Don’t forget though doing so will:

  • change the physics manager
  • require you bear in mind what layers you’re colliding with, casting against etc
  • require some understanding of bitmasks (not hard just requires it)