Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question NavMeshAgent.SetDestination doesn't move initially but if I move it once it will work.

Discussion in 'Navigation' started by Glister, Feb 5, 2023.

  1. Glister

    Glister

    Joined:
    Apr 15, 2013
    Posts:
    4
    So I have setup the NavMesh and the enemy will move on the mesh if I mouse click. My next thinking was to set a target for the capture point at the start and have it move towards the capture point. I moved the enemy to a distant location on the Navmesh. The NavMeshAgent is still well within the boundary of the NavMesh. After moving the starting position of the enemy it wont move via mouse click or by setting the destination to the target. The funny thing is if I go into scene in the editor and I slightly nudge the enemy to move it forward/backward or left/right then is picks up the destination and starts moving. I thought maybe it was in a weird place on the NavMesh and have moved it around to several spots with a similar result. Further if I change the obstacle avoidance to less than .01 it will move from it's starting position. Further once it's in motion it moves just fine avoiding the trees on the terrain and traversing to the capture point.

    This makes me wonder if the starting position was still the cause but it's standing in an open field. There is some slight variation to the terrain but minimal angles. I further wen on to change the bake options for navigations to allow for max slope of 60 and a jump height of 5 but no luck. The only thing that seems to let it move consistently is nudging manually or lowering the radius to much smaller than the capsule collider.

    Any thoughts on why this is happening would be greatly appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class MobOne : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private NavMeshAgent agent;
    10.     [SerializeField]
    11.     private Camera curCam;
    12.  
    13.     private GameObject CapturePoint;
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         CapturePoint = AcquireTarget();
    20.         //Debug.Log(CapturePoint);
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (Input.GetMouseButtonDown(2))
    27.         {
    28.             Ray movePosition = curCam.ScreenPointToRay(Input.mousePosition);
    29.             if (Physics.Raycast(movePosition,out var hitInfo))
    30.             {
    31.                 agent.SetDestination(hitInfo.point);
    32.                 //agent.SetDestination(CapturePoint.transform.position);
    33.             }
    34.         }
    35.         else if (agent.velocity == Vector3.zero)
    36.         {
    37.             //agent.SetDestination(CapturePoint.transform.position);
    38.             //Debug.Log(CapturePoint.transform.position);
    39.             MoveAgent();
    40.         }
    41.         //Debug.Log(agent.velocity == Vector3.zero);
    42.  
    43.     }
    44.  
    45.     private GameObject AcquireTarget()
    46.     {
    47.         GameObject curTarget;
    48.         curTarget = GameObject.FindGameObjectWithTag("CapturePoint");
    49.         //Debug.Log(curTarget);
    50.         return curTarget;
    51.        
    52.     }
    53.  
    54.     private void MoveAgent()
    55.     {
    56.         agent.SetDestination(CapturePoint.transform.position);
    57.     }
    58.  
    59. }
    60.