AI wont follow

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.

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

public class Enemy : MonoBehaviour
{
    private NavMeshAgent Mob;

    public GameObject Player;

    public float MobDistanceRun = 4.0f;

    // start is called before the first frame update
    void start()

    {
        Mob = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(transform.position, Player.transform.position);

        // run to player

        if (distance < MobDistanceRun)
        {
            Vector3 dirToPlayer = transform.position - Player.transform.position;

            Vector3 newPos = transform.position - dirToPlayer;

            Mob.SetDestination(newPos);
        }

    }
}

You only need to put in the coordinates of the place you want the agent to go to (as a vector3)

Mob.SetDestination(player transform position)

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
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

Maybe just test out this simple version before adding more

public class Enemy : MonoBehaviour
{
    private NavMeshAgent Mob;
    public GameObject Player;
    public float MobDistanceRun = 4.0f;
    // start is called before the first frame update
    void start()
    {
        Mob = GetComponent<NavMeshAgent>();
        Mob.SetDestination(Player.transform.position);
    }
}

This should make the agent move to the player when the game starts