For each Raycast?

I has this code here and it works but only on the one ray.

What it does is cast 3 rays out one goes left, one goes right and one right in front. Like a field of view, but only with 3 rays. I intend it to be like that now, its not supposed to be a sweep.

So yeah how to get the hits from all three rays to work with the if? I can only get the left one to work.

Vector3 forwd = headdummyBone.TransformDirection(Vector3.forward);
Vector3 left = headdummyBone.TransformDirection(new Vector3(-30, -2. 10));
Vector3 right = headdummyBone.TransformDirection(new Vector3(30, -2. 10));


RaycastHit hit;

if(Physics.Raycast(headdummyBone.position, fwd, out hit, maxSeeDistance, ignoringLayers)){
   

if(Physics.Raycast(headdummyBone.position, left, out hit, maxSeeDistance, ignoringLayers)) || Physics.Raycast(headdummyBone.position, right, out hit, maxSeeDistance, ignoringLayers)){

if(hit.transform.tag == "Player"){
   
   canFollow = true;


}}}}

This code is saying:

If the FRONT ray hits something AND the left OR right hits something, do this… etc.

That’s not right is it? canFollow = true; will only run when something intersects the front AND one of the two sides.

You also are using the same raycast hit, you need to use an array of RaycastHits, as suggested by someone else, or simply define three raycasthits for each direction…

Try something like this (NOTE, this is untested, this is simply to get you on the lines of what you need to do.)

RaycastHit hitLeft;
RaycastHit hitFwd;
RaycastHit hitRight;

// you are seperating each hit by an OR - so IF (front hits) OR (left) OR (right)
// also note, each raycast, needs a different hit to use.
if(Physics.Raycast(headdummyBone.position, -headdummyBone.right, out hitLeft, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.right, out hitRight, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.foward, out hitFwd, maxSeeDistance, ignoringLayers)) {
     
    // one of the rays hit something. Check each hit for tag too.
    
    if(hitFwd.transform.tag == "Player" || hitRight.transform.tag == "Player" || hitLeft.transform.tag == "Player"){
    
       // is the object it hit a player?
       canFollow = true;
     
     
    }
    
    }