- Home /
Unity Raycasting a tag rather than layer
I have made a game where I use raycasting to determine whether or not the player is on an object. I know how to do this using layers, but I'd rather make it so it checks if it hits an object with a tag. Heres the code:
isObject = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer ("Object"));
So rather than comparing to a layermask with the name "Object", I would rather it checked if it hits an object with a tag, but I do not know what to replace LayerMask with.
Answer by Owen-Reynolds · May 11, 2015 at 01:03 PM
You can't use tags to change the way a raycast works. A raycast stops at the first thing it hits, skipping things not in the layerMask. You can't make it skip things based on tag. In other words, if a raycast would hit A with tag "friendly", then B with tag "enemy", it will always hit A and stop. If you used layers, you could make it skip layer "friendly" and hit B; but now way to ignore A based on tags.
The best you can do is RaycastAll, which gives you a list of everything on the path. Then search through the list, skipping certain tags on your own (you should be able to find an example using raycastAll, which shows this.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613195318im_/https://answers.unity.com/themes/thub/images/avi.jpg)