Determine if object is between enemy and player

Hi,

I’m trying to determine if a object the enemy is using for cover is between him and the player in function gotoCover(), I’ve done the code for him to run for cover then face and shoot at the player, but if he’s in front of the object I want him to go behind, the enemy is using NavMeshAgent.

The code below I’ve chopped out of my AI for you to look over and used for testing FindCover -

Thanks.

function Update(){

FindCover(transform.position);

// Used to test on keyboard
if (Input.GetKeyDown ("c")){
    navAgent.speed = 6.0;
    navAgent.destination = nearestCover.transform.position;
    navAgent.stoppingDistance = 2;
    run();
    cover = true;
}

if(!cover){

        // Patrol, Chase, Attack

    }
    else gotoCover();
}

// -------------------------------------------------------

function FindCover(pos: Vector3) : GameObject{

// Get all colliders that intersect the transform.position:
        var cols = Physics.OverlapSphere(pos, lookForCoverRange);
// Find the nearest one:
        var dist: float = Mathf.Infinity;
    for (var col: Collider in cols){
// Find the distance to pos:
        var d = Vector3.Distance(pos, col.transform.position);
    if ((d < dist) && (col.gameObject.tag == "Concrete Barrier")){  // If closer and it's a Concrete Barrier..
        dist = d;                                                   // Save its distance ..
        remainingDistance = dist;                                   // Remaining distance to cover = distanct to closest cover object
        nearestCover = col.gameObject;                              // And its gameObject
        }
    }
    //print(nearestCover + " " + dist);
    return nearestCover;
}

// -------------------------------------------------------

function OnDrawGizmos(){

    Gizmos.color = Color.blue;
//Draws a sphere to see the detectionRange of Physics.OverlapSphere
    Gizmos.DrawWireSphere (transform.position, lookForCoverRange);
}

// -------------------------------------------------------

function gotoCover(){

    if(remainingDistance < 3){

    if(remainingDistance < 3 && !inCover){
        runningToSquatShootOnce();
            inCover = true;
                }
    gameObject.SendMessage (characterCollider2);
    repeatSquatShooting();

    shootPlayer();

    }
}

// -------------------------------------------------------

function shootPlayer(){

    if(!animation.IsPlaying("Run")){                                    // Only fireGunSound & muzzel flash once the terrorist is ataking
        if(!animation.IsPlaying("Stand to Lying Shooting Once")){       // Only fireGunSound & muzzel flash once the terrorist is lying down
            if(!animation.IsPlaying("Running to Squat Shoot Once")){    // Only fireGunSound & muzzel flash once the terrorist is Squating
                if(!animation.IsPlaying("Stand Shoot Once")){           // Only fireGunSound & muzzel flash once the terrorist is Standing and repeat firing

// Rotate to face the Player with added degrees of Y rotation to line up the Terrorist with the Player

var lookPos = player.position - transform.position;
    lookPos.y = 0;
    var rotation = Quaternion.LookRotation(lookPos);
    rotation *= Quaternion.Euler(0, attackDegrees, 0); // Use this to add degrees to line up terrorist with player when attacking
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turnToLookSpeed);

// Add degrees of Y rotation to line up the RayCast with the Player

// Check to see if the Terrorist is shooting the Player
// Vector3 lifts the Ray 0.6mt, and shoots to the players chest position

var targetRotation = Quaternion.Euler (0, rayDegrees, 0);
    rayPoint.localRotation = Quaternion.Slerp(rayPoint.localRotation, targetRotation, Time.time);

var fwd = rayPoint.TransformDirection (Vector3.forward) * 100;
    Debug.DrawRay (rayPoint.position + Vector3(0, -0.4, 0), fwd, Color.blue);

if (Physics.Raycast (rayPoint.position + Vector3(0, -0.4, 0), fwd, hit, shootDistance + 1)){

    if(hit.collider.gameObject.tag == "Player"){
        playerDamage.GetComponent(PlayerDamage).damage();
        muzzelFlashing = true;
        fireGunSound();
        globalVars.GetComponent(GlobalVars).terroristShooting[terroristNumber] = true;
    }
}
                }
            }
        }
    }
}

// -------------------------------------------------------

Lotta code, but, when you shoot, you already have the raycast to see if the player is blocked by anything.

What’s wrong with using the “opposite” raycast for “am I in cover”?

Sine the player can aim anywhere, you might eventually do it from your head, left shoulder and right shoulder.