Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

AI doesn't have any trigger to see a player and follow it

Discussion in 'Navigation' started by ethannsmoo, Oct 25, 2020.

  1. ethannsmoo

    ethannsmoo

    Joined:
    Nov 23, 2019
    Posts:
    3
    So I have a script that he patrols then when he spots the player he follows the player everything works BUT he cant spot the player I have to manually click a button can someone help im very new to scripting.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. public class Enemy : MonoBehaviour
    6. {
    7.      //Variables
    8.      public Transform[] patrolPoints;
    9.      private int destPoint = 0;
    10.      private NavMeshAgent agent;
    11.      public bool playerInRadius;
    12.      public int speed;
    13.      public GameObject player;
    14.      public EnemyTypes eCurState = EnemyTypes.Patrol;
    15.      void Start ()
    16.      {
    17.          //Sets NavMeshAgent
    18.          agent = GetComponent<NavMeshAgent> ();
    19.      }
    20.      void GoToNextPoint ()
    21.      {
    22.          //If no patrol points are set
    23.          if (patrolPoints.Length == 0)
    24.          {
    25.              return;
    26.          }
    27.          //Sets the destination to all the different positions stated in the inspector
    28.          agent.destination = patrolPoints [destPoint].position;
    29.          destPoint = (destPoint + 1) % patrolPoints.Length;
    30.      }
    31.      void FixedUpdate ()
    32.      {
    33.          //If the player is in the detection radius
    34.          if (playerInRadius)
    35.          {
    36.              //Chase
    37.              eCurState = EnemyTypes.Chase;
    38.          }
    39.          //If the player is not
    40.          if (!playerInRadius)
    41.          {
    42.              //Go back to patrolling
    43.              eCurState = EnemyTypes.Patrol;
    44.          }
    45.          switch (eCurState)
    46.          {
    47.          case EnemyTypes.Chase:
    48.              HandleChase ();
    49.              break;
    50.          case EnemyTypes.Patrol:
    51.              HandlePatrol ();
    52.              break;
    53.          }
    54.      }
    55.      public enum EnemyTypes
    56.      {
    57.          Patrol,
    58.          Chase
    59.      }
    60.      void HandlePatrol ()
    61.      {
    62.          if (!agent.pathPending && agent.remainingDistance < 0.5f)
    63.          {
    64.              GoToNextPoint ();
    65.          }
    66.      }
    67.      //Handles chase
    68.      void HandleChase ()
    69.      {
    70.          agent.destination = player.transform.position;
    71.      }
    72. }
    73.  
     
  2. the_0dium

    the_0dium

    Joined:
    Nov 29, 2018
    Posts:
    15
    That's because you haven't set the way the enemy spots the player. For example you should check the distance between the player and the enemy and if it is small enough to spot the player, playerInRadius should be changed to true and if the player is too far then to false. Then it will work.
     
unityunity