- Home /
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[i].transform.root != trgt && hits[i].transform.root != this.gameObject
&& hits[i].distance < wallDist) {
wallDist = hits[i].distance;
}
//if hits target, set tarDist
if (hits[i].transform.root == trgt) {
tarDist = hits[i].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.
Answer by Tommynator · Mar 01, 2012 at 01:02 PM
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?
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
It says Collider.Raycast ignores all colliders except itself, which is kind of the opposite of what I need.
Answer by Owen-Reynolds · Mar 01, 2012 at 03:26 PM
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.)
Your answer
Follow this Question
Related Questions
Usage of Layer masks, what am I doing wrong? 1 Answer
Help with Layermasks 1 Answer
How do I use layermasks? 9 Answers
Layer mask doesn't work... 1 Answer