x


Field of view, using raycasting

I am making a AI script. I have already made it so the script checks if the enemy can see the player, based on a raycast that checks if there are any obsticals obscuring the enemy's view of the player.

function CanSeePlayer() : boolean{

    var hit : RaycastHit;
    var rayDirection = playerObject.transform.position - transform.position;

    if (Physics.Raycast (transform.position, rayDirection, hit)) {

        if (hit.transform.tag == "Player") {
            Debug.Log("Can see player");
            return true;
        }else{
            Debug.Log("Can not see player");
            return false;
        }
    }
}

The problem is that this gives the enemy a 360 degree view, how can i restrict it so the enemy can only see the player if the player is within a 135 degree cone in front of the enemy?

more ▼

asked Apr 22 at 10:57 AM

Mattivc\'s gravatar image

Mattivc
1.1k 51 57 63

(comments are locked)
10|3000 characters needed characters left
 moderation talk

4 answers:

I found a solution to the problem, i used Vector3.Angle to detect how far away from the front of the enemey the player is. The code ended upp looking like this for anyone that migth be interested:

function CanSeePlayer() : boolean{

    var hit : RaycastHit;
    var rayDirection = playerObject.transform.position - transform.position;

    if(Physics.Raycast (transform.position, rayDirection, hit)){ // If the player is very close behind the player and in view the enemy will detect the player
        if((hit.transform.tag == "Player") && (distanceToPlayer <= minPlayerDetectDistance)){
        return true;
        }
    }

    if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange){ // Detect if player is within the field of view
        if (Physics.Raycast (transform.position, rayDirection, hit)) {

            if (hit.transform.tag == "Player") {
                //Debug.Log("Can see player");
                return true;
            }else{
                //Debug.Log("Can not see player");
                return false;
            }
        }
    }
}
more ▼

answered Apr 22 at 12:20 PM

Mattivc\'s gravatar image

Mattivc
1.1k 51 57 63

(comments are locked)
10|3000 characters needed characters left
 moderation talk

Thanks Mattivc!! This helped me a lot... I tried to figure out what some of your variables are since not all of them were declared in your code snippet. Here's my slightly modified version if anyone wants to check out. Just attach this javascript script to the enemy, drag your player to the playerObject variable, and be sure the player is tagged "Player"

var playerObject : GameObject; // the player
var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees)
var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being deteacted
var rayRange : float; // distance the enemy can "see" in front of him
private var rayDirection = Vector3.zero;

function CanSeePlayer() : boolean
{
    var hit : RaycastHit;
    rayDirection = playerObject.transform.position - transform.position;
    var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);

    if(Physics.Raycast (transform.position, rayDirection, hit)){ // If the player is very close behind the enemy and not in view the enemy will detect the player
        if((hit.transform.tag == "Player") && (distanceToPlayer <= minPlayerDetectDistance)){
            //Debug.Log("Caught player sneaking up behind!");
            return true;
        }
    }


    if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange){ // Detect if player is within the field of view
        if (Physics.Raycast (transform.position, rayDirection, hit, rayRange)) {

            if (hit.transform.tag == "Player") {
                //Debug.Log("Can see player");
                return true;
            }else{
                //Debug.Log("Can not see player");
                return false;
            }
        }
    }
}


function OnDrawGizmosSelected ()
{
    // Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy
    Gizmos.color = Color.magenta; // the color used to detect the player in front
    Gizmos.DrawRay (transform.position, transform.forward * rayRange);
    Gizmos.color = Color.yellow; // the color used to detect the player from behind
    Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance);      
}
more ▼

answered Jul 01 at 12:30 AM

Jordan Thompson\'s gravatar image

agent_smith
181 16 22 27

(comments are locked)
10|3000 characters needed characters left
 moderation talk

Hi. I'm trying to implement this code but I can't get it to work for some reason.

I'm learning 3d modelling and have no experience of coding, so I'm really in the dark as to what to do. I'm trying to get the enemy to look at the character if the code conditions are true, and have added the code transform.LookAt(target); but it doesn't work.

I've tagged the player object as Pill, dragged the Player object onto the target parameter in the script option so it knows what to look at, and dragged the script onto the enemy sentry. Please help, as I'm pulling my hair out and I'm sure it's something blindingly simple. If you could tell me exactly what to do it'b be brilliant - I'm still just finding my way around Unity at the moment;

var playerObject : GameObject; // the player

var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees) var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being deteacted var rayRange : float; // distance the enemy can "see" in front of him private var rayDirection = Vector3.zero; var target : Transform;

function CanSeePlayer() : boolean { var hit : RaycastHit; rayDirection = playerObject.transform.position - transform.position; var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);

if(Physics.Raycast (transform.position, rayDirection, hit)){ // If the player is very close behind the enemy and not in view the enemy will detect the player
    if((hit.transform.tag == "Pill") && (distanceToPlayer <= minPlayerDetectDistance)){
        //Debug.Log("Caught player sneaking up behind!");
        transform.LookAt(target);
        return true;

    }
}


if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange){ // Detect if player is within the field of view
    if (Physics.Raycast (transform.position, rayDirection, hit, rayRange)) {

        if (hit.transform.tag == "Pill") {
            //Debug.Log("Can see player");
            transform.LookAt(target);
            return true;


        }else{
            //Debug.Log("Can not see player");
            return false;
        }
    }
}

}

function OnDrawGizmosSelected ()

{ // Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy Gizmos.color = Color.magenta; // the color used to detect the player in front Gizmos.DrawRay (transform.position, transform.forward * rayRange); Gizmos.color = Color.yellow; // the color used to detect the player from behind Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance);
}

more ▼

answered Oct 03 at 10:18 AM

copperspock\'s gravatar image

copperspock
1 1 1 1

(comments are locked)
10|3000 characters needed characters left
 moderation talk

Thanks both of you, this is usefull as there are not much code for this.

I'm going to try it.

more ▼

answered Aug 11 at 10:05 AM

mouha\'s gravatar image

mouha
33 3 3 8

(comments are locked)
10|3000 characters needed characters left
 moderation talk
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x798
x561
x104
x81
x13

Asked: Apr 22 at 10:57 AM

Seen: 4925 times

Last Updated: Aug 03 at 08:52 PM

powered by Qato - Enterprise Q&A