nav mesh agent completely broken

I’m making a vr game like gorilla tag and I want the player to be chased by something (rn just a sphere) and I thought I got it to work so that it chases the closest player but when the player gets spawned in it just goes to were it spawned and dosent update and when it moves it goes crazy and completely breaks

here’s the script to find the closest player and chase them\

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

public class ai : MonoBehaviour
{

    int closestPlayerIndex = 0;

    float distanceMin = float.MaxValue;

    public GameObject[] players;

    public NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        players = GameObject.FindGameObjectsWithTag("Player");
        targetplayer();
        agent.updatePosition = true;

    }

    void targetplayer()
    {
       

        for (int i = 0; i < players.Length; i++)
        {
            float distanceFromPlayer = Vector3.Distance(players*.transform.position, transform.position);*

if (distanceFromPlayer < distanceMin)
{
distanceMin = distanceFromPlayer;
closestPlayerIndex = i;
Debug.Log(closestPlayerIndex);
}
}
}

private void FixedUpdate()
{
agent.destination = players[0].transform.position;
}
}

Two things:


distanceMin is never reset, so its getting smaller every time a player gets closer than distanceMin.


When setting the agent destination, you are not using your calculated index, but players[0].

Is there a special reason why you are doing this in FixedUpdate() and not right after getting the nearest player?


btw

agent.updatePosition = true doesn’t need to be called on every update and the default is true anyway ([Doc][1]).


If, for some reason, no player is in play at some point, your solution will break, so you might want to check this when setting the destination :wink:


Update: This works for me:

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

public class ai : MonoBehaviour
{

    int closestPlayerIndex = 0;

    float distanceMin = float.MaxValue;

    public GameObject[] players;

    public NavMeshAgent agent;

    float targetplayerUpdateInterval = 0.25f;

    float targetUpdateTimer;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        targetplayer();
    }

    void targetplayer()
    {
        if (targetUpdateTimer > 0)
        {
            targetUpdateTimer -= Time.deltaTime;
            return;
        }

        targetUpdateTimer = targetplayerUpdateInterval;

        players = GameObject.FindGameObjectsWithTag("Player");

        if (players.Length == 0)
        {
            agent.destination = Vector3.zero;
            return;
        }

        for (int i = 0; i < players.Length; i++)
        {
            float distanceFromPlayer = Vector3.Distance(players*.transform.position, transform.position);*

if (distanceFromPlayer < distanceMin)
{
distanceMin = distanceFromPlayer;
closestPlayerIndex = i;
}
}

agent.destination = players[closestPlayerIndex].transform.position;

distanceMin = float.MaxValue;
}

void OnCollisionEnter(Collision other)
{
Destroy(other.gameObject);
}
}
[1]: Unity - Scripting API: AI.NavMeshAgent.updatePosition

thank you for the answer I put it in fixed update because I thought maybe the for loop was going forever and not letting the set position go