collider line of sight

hello ,
I was wondering if there is a good way to check line of sight between colliders.
This could be useful in cases where say an AI npc cant see the center of a player but can see there arm of some other part of the player that sticks out.

scripts in c# please.

Thanks in advance.

No scripts in C#, just check the documentation for raycasts. The easiest way to accomplish what you need is to place ‘targets’ on the extremities of your player, like one on each shoulder, one on each hand, one on the head, one on the chest, etc, and register each of these in a script on the main player object. As long as the enemies have a way of knowing who the players are (some sort of static GameManager where the player objects register themselves when they start, or a cone-shaped “vision range” trigger area where you can store the last-seen players when they walk into it, etc), they should be able to retrieve this list and use it. They can then raycast at these targets each frame, or every X frames, or alternating each raycast in a different frame (latter two for performance reasons). The source of those raycasts would likely just be the face/eyes of the model. The ‘targets’ are not necessarily colliders (just transform locations), but can be inside of colliders if you want to keep it dead-simple. Raycast against each target, if any player collider is hit, then the enemy can ‘see you’, but if anything else is hit, they can’t.

Alternatively, you can EXCLUDE the player collision layer from the raycast. In that case, the ‘targets’ on the player don’t necessarily need to be inside of colliders (if, for instance, you only have a chest collider because you don’t want to be able to shoot peoples’ hands, ie: laser tag). The raycast-firing script can just calculate the distance between its own position and the target, fire rays only if that distance is not greater than the max sight range, set the max ray distance as the distance between the enemy and the player target, and only count “hits” if NOTHING is hit by the raycast (ie: no other collider is in the way between those two points). In the situation where the enemy has a reference to the player already stored (which it would have, if you’re cycling specific targets this way), then you don’t actually need the raycast hit information.

Hope that gives you some ideas. If you need code / demonstrations, just google “Unity raycast example project”, or check the project demos on the Unity Learn section of the website. They’re incredibly easy to find.