- Home /
NetworkNavMeshAgent freezes agent when correcting
Context
Hi, I am using the relatively new NetworkNavMeshAgent script from the MLAPI networking package. I am having issues with using the NetworkNavMeshAgent
the developers provide. The agent will freeze whenever it is following a path when a correction occurs.
Player prefab setup
I have set up a player prefab that spawns when a client connects. It has the following scripts:
Transform (default settings)
CapsuleMeshFilter (default settings)
CapsuleCollider (default settings)
MeshRenderer (default settings)
NetworkObject (default settings)
NavMeshAgent (default settings)
NetworkNavMeshAgent (default settings)
PlayerMovementController (My own script)
My PlayerMovementController
script looks as follows:
using MLAPI;
using MLAPI.Messaging;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMovementController : NetworkBehaviour
{
private NavMeshAgent _navMeshAgent;
public override void NetworkStart()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
}
public void Update()
{
if (IsLocalPlayer)
{
if (!Input.GetMouseButtonDown(1)) return;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var mask = LayerMask.GetMask("Ground");
if (!Physics.Raycast(ray, out var hit, Mathf.Infinity, mask)) return;
SetNavMeshAgentDestinationServerRpc(hit.point);
}
}
[ServerRpc]
private void SetNavMeshAgentDestinationServerRpc(Vector3 destination)
{
_navMeshAgent.SetDestination(destination);
}
}
In short: when the local player right-clicks, it casts a ray and sets the agent's destination on the server. I believe this is how it should work, but the documentation on the NetworkNavMeshAgent
is very minimal, and understandably so (since its brand-new).
The problem
When the destination is changed (by a right-click), the agent will start moving to that location. It all works fine forever, UNLESS a correction happens during the movement of the agent. This correction is handling desync issues and is fully handled within the NetworkNavMeshAgent
script. When this happens, the agent will stop following the path and be stuck, even if the destination is changed again. The only way to get the agent to move again is to wait untill two more corrections happen (???) and then a right click will have effect on the agent's movement again, unless the above event happens again.
Any ideas on how to resolve this issue? Am I using the NetworkNavMeshAgent
in an unintended way?
I just came across the exact same issue, which is exceptionally strange since the "official" way to follow a target is to set the destination every so often.
Would love to know the answer:P though for now the networkedtransform also works. but the behaviour is weird for sure.