RayCast vision testing for same layer objects?

I have multiple enemy objects with multiple hitboxes around the limbs, all in the same layer. The problem with testing their vision to the player or other enemies is that the raycast will collide with its own hitbox, or if layermasked, ignore other enemies.

This is the solution I tried:

	var hits : RaycastHit[];
sightPoint.LookAt(trgt);
//var trgtDir : Vector3 = Vector3.Normalize(trgt.position - transform.position);
transY = transform.position.y;

hits = Physics.RaycastAll (sightPoint.position, sightPoint.forward, 20, layerMask);
//test all hits in line, test obstacle distance against target distance
var wallDist : float = 100;
var tarDist : float = 0;
for (i=0; i<hits.length;i++) 
{
	//if hits wall, set wallDist
	if (hits_.transform.root != trgt && hits*.transform.root != this.gameObject*_

_ && hits*.distance < wallDist) {
wallDist = hits.distance;
}
//if hits target, set tarDist*

if (hits*.transform.root == trgt) {
tarDist = hits.distance;
}
}
if (tarDist != 0 && tarDist < wallDist ) {
lastSeenPos = trgt.position;
return true; }
else {
return false; }*

It seems very unreliable and randomly fails even when the target is clearly in sight. There has to be a better way._

Is it really necessary to use RaycastAll? If not, try using the Raycast function that you get within the Collider class. So you could go through all you Head Hitbox colliders of your enemies and run the `collider.Raycast()` function - it's ignoring collisions with the calling collider by default.

An alternative (not very nice) solution is to set the layer of the enemy you are casting from temporarily to `IgnoreRaycast`. After the raycast you can set it back to normal. Not sexy but should work.

Btw - I didn't understand what you are trying to do with the wallDist checking. Is it to avoid false positives - e.g. the target is behind a wall?

No matter what method you use, add one more check for if you miss everything (in your case, hits is empty.) It sounds odd, but if a ray says there’s nothing between me and you, I can see you.

What happens, as the math and colliders get stranger, is you raycast through an armpit, or one of you is leaning back, so your 20 meter ray stops just in front of their face (and increasing the raycast distance makes “through the armpit” hit the wall behind them.)