Search Unity

AI wont follow

Discussion in 'Navigation' started by spiretakk, May 28, 2021.

  1. spiretakk

    spiretakk

    Joined:
    Aug 18, 2019
    Posts:
    10
    Hello everyone !

    So i attached this script into a capsule so i can test some AI / follow player movements , attached a nav mesh to the capsule and baked the nav mesh. No errors in the script. Only thing is that the capsule wont follow the player. Im providing the script so you can have a look ! Thanks in advance. Im using 2019.4.26f.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class Enemy : MonoBehaviour
    8. {
    9.     private NavMeshAgent Mob;
    10.  
    11.     public GameObject Player;
    12.  
    13.     public float MobDistanceRun = 4.0f;
    14.  
    15.     // start is called before the first frame update
    16.     void start()
    17.  
    18.     {
    19.         Mob = GetComponent<NavMeshAgent>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         float distance = Vector3.Distance(transform.position, Player.transform.position);
    26.  
    27.         // run to player
    28.  
    29.         if (distance < MobDistanceRun)
    30.         {
    31.             Vector3 dirToPlayer = transform.position - Player.transform.position;
    32.  
    33.             Vector3 newPos = transform.position - dirToPlayer;
    34.  
    35.             Mob.SetDestination(newPos);
    36.         }
    37.  
    38.     }
    39. }
    40.  
    41.  
     
  2. boffer

    boffer

    Joined:
    Oct 28, 2020
    Posts:
    2
    You only need to put in the coordinates of the place you want the agent to go to (as a vector3)
    Code (CSharp):
    1. Mob.SetDestination(player transform position)
    https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.SetDestination.html

    There could be a number of different issues that are not code related

    - The agent or the target is too far away from the navmesh
    - The agent can't make a path (dosen't fit through a corridor/hole, it is obstructed)
    Open Window->AI->Navigation and look at the navmesh to make sure it has baked in a sensible way

    Code (CSharp):
    1. public float MobDistanceRun = 4.0f;
    This could be a different value than 4, since it is serialized, make sure it is not set to 0 or something else


    I would set the agent speed to a value instead of leaving it up to default
    https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-speed.html


    Maybe just test out this simple version before adding more

    Code (CSharp):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     private NavMeshAgent Mob;
    4.     public GameObject Player;
    5.     public float MobDistanceRun = 4.0f;
    6.     // start is called before the first frame update
    7.     void start()
    8.     {
    9.         Mob = GetComponent<NavMeshAgent>();
    10.         Mob.SetDestination(Player.transform.position);
    11.     }
    12. }
    This should make the agent move to the player when the game starts