- Home /
How to allow a ray-cast through a player?
I am creating an item pick up script, and the main purpose for it is to check the tag and info on another script attached to a game object or the item. What is happening to the ray though is it is passing through the player, my question is how do i allow a raycast through the player object?
Thank you ahead of time.
-Christian J
Do what Johat suggested, or use RaycastAll and iterate through the hit colliders until you've found the one you need.
Answer by Johat · Apr 20, 2016 at 06:40 AM
The simplest way to achieve this would probably be through a LayerMask. See the Physics.Raycast reference here.
If you put the player on a different layer to the GameObjects you want to check against, then you can mask that out to ignore it.
I would have a field in your script of type LayerMask since this will give you a nice drop-down in the inspector -- you can choose multiple layers to ignore easily if you like.
Something like:
public LayerMask IgnoreLayers;
Then your raycast could be something like this (check the reference for the version of raycast you want to call):
// Bitwise not -- ~ -- flips all the bits of the integer in this case, to allow every layer EXCEPT those you selected in the LayerMask
Physics.Raycast(someOrigin, someDirection, someMaxDistance, ~IgnoreLayers.value);