NavMesh is not called

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

public class RTSMovement : NetworkBehaviour
{
    [SerializeField] private NavMeshAgent agent = null;
    private Camera mainCamera;

    #region Server

    [Command]
    private void CmdMove(Vector3 position)
    {
        if (NavMesh.SamplePosition(position, out NavMeshHit hit, 1f, NavMesh.AllAreas)) { return; } 
        {
            agent.SetDestination(hit.position);
        }
    }
    #endregion

    #region Client

    public override void OnStartAuthority()
    {
        mainCamera = Camera.main;
    }

    [ClientCallback]
    private void Update()
    {
        Debug.Log("A");
        if(!isOwned) { return; }
        if (!Input.GetMouseButton(1)) { return; }
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        if(!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity)) { return; }
        CmdMove(hit.point);
    }

    #endregion
}

I am pretty sure there are no issues in my code, but when I test this the debug is not called wish is logical because we are not using mono behavior and the method name does not become yellow. For some reason in the video the text does become yellow and I assume the method as a whole is called as well…

Small edit, the update function does get called but my character still does not move.

You are returning if the position is valid. It will not move

1 Like

Awesome, that did the trick. I am however wondering is it better to add an ! before navmesh.sample… or to remove return segment?

1 Like

Currently it makes no difference. You may want to add some other code in here later where it may matter, but as it is now it’s the same thing. I would personally add the ! and return early but that’s because I tend to write code for ‘just in case’.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms