Search Unity

Question How do I make my Enemy AI back up?

Discussion in 'Scripting' started by StardomSwords, Apr 11, 2021.

  1. StardomSwords

    StardomSwords

    Joined:
    Apr 11, 2021
    Posts:
    1
    Hello! I am currently working on a small game and I am trying to make on of my AIs Back up if the player is too close, but I do not know how to go about it, I have this code so far...
    Code (CSharp):
    1. public class Range_AI : MonoBehaviour
    2. {
    3.  
    4.     public NavMeshAgent agent;
    5.  
    6.     public Transform Player;
    7.  
    8.     public LayerMask whatisGround, whatisPlayer;
    9.     //Patroling AI
    10.     public Vector3 walkPoint;
    11.     bool walkPointSet;
    12.     public float walkPointRange;
    13.  
    14.  
    15.     //A sad yet funny attempt to attack player
    16.     public float TBA; //No, not *that* kind of TBA, TBA is short for "time between attack"
    17.     bool Attacked;
    18.     public GameObject projectile;
    19.  
    20.    // (States)
    21.     public float sightRange;
    22.     public float attackRange;
    23.     public float BackUpRange;
    24.     public bool PIS;
    25.     public bool PIAR; //"Player in attack range"
    26.     public bool PITC; //"Player is too close"
    27.  
    28.     private void Awake()
    29.     {
    30.         Player = GameObject.Find("Player").transform;
    31.         agent = GetComponent<NavMeshAgent>();
    32.      
    33.     }
    34.  
    35.     private void Update()
    36.     {
    37.         PIS = Physics.CheckSphere(transform.position, sightRange, whatisPlayer);
    38.         PIAR = Physics.CheckSphere(transform.position, attackRange, whatisPlayer);
    39.         PITC = Physics.CheckSphere(transform.position, BackUpRange, whatisPlayer);
    40.  
    41.         if (!PIS && !PIAR && !PITC)
    42.         {
    43.             Patroling();
    44.         }
    45.  
    46.         if (PIS && !PIAR && !PITC)
    47.         {
    48.             Chase();
    49.         }
    50.  
    51.         if (PIS && PIAR && !PITC)
    52.         {
    53.            Attack();
    54.         }
    55.         if (PIS && PIAR && PITC)
    56.         {
    57.            BackUp();
    58.         }
    59.  
    60.    
    61.     }
    62.      void Patroling()
    63.     {
    64.         if (!walkPointSet)
    65.         {
    66.             FindPoint();
    67.         }
    68.  
    69.         if (walkPointSet)
    70.         {
    71.             agent.SetDestination(walkPoint);
    72.         }
    73.  
    74.         Vector3 distanceToWalkPoint = transform.position - walkPoint;
    75.  
    76.         //Entity reached Walkpoint
    77.         if(distanceToWalkPoint.magnitude < 1f)
    78.         {
    79.             walkPointSet = false;
    80.         }
    81.     }
    82.  
    83.     private void FindPoint()
    84.     {
    85.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    86.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    87.  
    88.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    89.  
    90.         if (Physics.Raycast(walkPoint, -transform.up, 2f, whatisGround))
    91.         {
    92.             walkPointSet = true;
    93.         }
    94.     }
    95.  
    96.      private void Chase()
    97.     {
    98.         agent.SetDestination(Player.position);
    99.     }
    100.      private void Attack()
    101.     {
    102.         agent.SetDestination(transform.position);
    103.  
    104.         transform.LookAt(Player);
    105.  
    106.         if (!Attacked)
    107.         {
    108.             //Attack type: Range
    109.          
    110.             Attacked = true;
    111.             Invoke(nameof(ResetAttack), TBA);
    112.         }
    113.     }
    114.  
    115.     private void BackUp()
    116.     {
    117.         transform.LookAt(Player);
    118.        
    119.     }
    120.  
    121.  
    122.     private void ResetAttack()
    123.     {
    124.         Attacked = false;
    125.     }
    126.  
    127. }
    Any help will be much appreciated!!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Backing up can be tricky. Unlike saying "go here" you have to say "go somewhere that is NOT here..."

    Lots of ways to choose that "not here" location:

    - just back away from the player (problem: you can back enemies into a corner retreating)

    - calculate a vector away from the player (problem: it might be out of bounds or otherwise unreachable)

    - calculate a random place further and further away (on the same side of the player) until you find somewhere to go (problem: enemies can make weird unlikely maneuvers)

    I would try a few different ways, see what feels best given the tightness of your playfields, the speeds involved (player and enemy), etc.
     
    StardomSwords likes this.