Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help making my AI stop following its destination

Discussion in 'Scripting' started by mattiascombitech, Jul 20, 2022.

  1. mattiascombitech

    mattiascombitech

    Joined:
    Jun 15, 2022
    Posts:
    5
    Hey!
    So basically I am trying to make a car game and I am currently working on the AI. I applied a script to the AI which can detect the playercar. I also used a waypointsystem and made the AI follow it. However, I also wish to make the AI stop following the waypointsystem when detecting the player car, but I am uncertain on how.

    This is the script I am currently working on:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class NPC : MonoBehaviour
    7. {
    8.     [HideInInspector] public AISensor sensor; // This is the code for the sensor which I can provide if necessary
    9.  
    10.     public GameObject destinationPoint;
    11.     NavMeshAgent theAgent;
    12.     GameObject brakes;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         sensor = GetComponent<AISensor>();
    18.         theAgent = GetComponent<NavMeshAgent>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         theAgent.SetDestination(destinationPoint.transform.position); // This is to make the AI follow the waypointsystem
    24.     }
    25.     public void Update(NPC npc)
    26.     {
    27.         if (!brakes)
    28.         {
    29.             brakes = FindPickUp(npc);
    30.  
    31.             if (brakes)
    32.             {
    33.                  = false;  // I don't know what to add in front of false
    34.             }
    35.         }
    36.     }
    37.  
    38.     GameObject FindPickUp(NPC npc)
    39.     {
    40.         if (npc.sensor.Objects.Count > 0)
    41.         {
    42.             return npc.sensor.Objects[0];
    43.         }
    44.         return null;
    45.     }
    46. }
     
  2. mattiascombitech

    mattiascombitech

    Joined:
    Jun 15, 2022
    Posts:
    5
    I appreciate all suggestions and tips, and I can provide more information if needed
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Can you not just set a new destination or disable the NavMeshAgent?
     
  4. mattiascombitech

    mattiascombitech

    Joined:
    Jun 15, 2022
    Posts:
    5
    Yea, I will try to set a new destination, cuz then I can also chose where it goes