How to evade Enemy AI?

So I have my enemy AI (police car) made of three main scripts, the pursuit, patrol and field of view. At the moment I have it so that when the player enters the line of sight and view range the police car enters pursuit. My problem is I’m not sure how to make it so that the player can evade the police AI. I was planning on using something like if dstToTarget > 150? along with setting it up so that if the target is no longer visible wait 30 seconds before abandoning but have no idea how to go about it. Any help would be awesome, thanks in advance!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FieldOfView : MonoBehaviour
{
    NavMeshAgent agent;

    public bool inPursuit = false;

    public bool test = false;

    public Transform target;
    public Transform self;

    public Patrol scriptPatrol;
    public Follow scriptPursuit;
    public NavMeshAgent scriptNavAgent;

    public bool InPursuit = false;
    public bool InPatrol = true;

    public float viewRadius;
    [Range(0, 360)]
    public float viewAngle;

    public LayerMask targetMask;
    public LayerMask obstacleMask;

    public GameObject PoliceLights;
    public GameObject PoliceSiren;
   

    [HideInInspector]
    public List<Transform> visibleTargets = new List<Transform>();

    void Start()
    {
        scriptPursuit = GetComponent<Follow>();
        scriptPatrol = GetComponent<Patrol>();
        scriptNavAgent = GetComponent<NavMeshAgent>();
       

        InPatrol = true;
        InPursuit = false;
        StartCoroutine("FindTargetsWithDelay", .2f);
    }


    IEnumerator FindTargetsWithDelay(float delay)
    {
        while (true)
        {
            yield return new WaitForSeconds(delay);
            FindVisibleTargets();
        }
    }


    void FindVisibleTargets()
    {
        visibleTargets.Clear();
        Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);

        for (int i = 0; i < targetsInViewRadius.Length; i++)
        {
            Transform target = targetsInViewRadius*.transform;*

Vector3 dirToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2)
{
float dstToTarget = Vector3.Distance(transform.position, target.position);

if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
{
visibleTargets.Add(target);
print(“I am in pursuit of the suspect!”);
scriptNavAgent.enabled = true;
scriptPatrol.enabled = false;
scriptPursuit.enabled = true;
InPatrol = false;
InPursuit = true;
PoliceLights.SetActive(true);
PoliceSiren.SetActive(true);
inPursuit = true;
}

if (dstToTarget < 20)
{
agent.speed = 5;
}

if (dstToTarget < 5)
{
agent.speed = 1;
}

if (dstToTarget < 1)
{
agent.speed = 0;
}

}

}
}

public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.y;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}

That depends a lot on what your desired outcome is. You proposed a valid solution to the problem, which is taking player distance into account.

You can also consider obstacles blocking the AI’s line of sight, by tracing a ray from the AI to the player and check if it intersects any blocking object. That would be a very quick and dirty way of doing it.

You could also implement “hiding” spots so that when the player walks into a trigger marked as “hide”, while being out of sight of the AI, then the AI won’t recover line of sight with the player.

But, of course, first of all, you need the define the expected behaviour, and then get to code it :slight_smile: