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._
Yeah, the hits are out of order so I could only think to compare the lowest distance of the wall to the distance of the target
– RyelandIt says Collider.Raycast ignores all colliders except itself, which is kind of the opposite of what I need.
– Ryeland