How to make an advanced line of sight?

Hi, does anybody know how to create an advanced line of sight for AI? Featuring that the visibility length fades out instead of just suddenly stop and that the AI responds differently depending how much of the object it sees?
Hope you understand the question :smiley:

thanks :smiley:

The easiest would be to create a normal line of sight and then check for distance when the AI sees the object and having the AI react differently depending on how close it is.

Example:

float distance = Vector3.Distance(transform.position, visibleObject.position);

if(distance < 10)
{
    //Object Close - Go to chase mode
}
else if(distance < 20)
{
    //Object Spotted - Go to alerted mode
}
else if(distance < 30)
{
    //Object Nearby - Go to investigate mode
}

This does not check for how much of the object is visible though, which is a lot more complicated to do.

Assuming you’re using raycasting, then the distance property of the RaycastHit returned will give the distance from the ray’s origin to the collision point. You can then set up conditional code blocks to act based on that value.