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

NavmestAgent SetDestination Doest work

Discussion in 'Navigation' started by hazuhiro, Oct 5, 2019.

  1. hazuhiro

    hazuhiro

    Joined:
    Feb 14, 2019
    Posts:
    34
    hi guys,
    i create navmeshagent and use setdestination but my character doesn't move

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class UnitManager : MonoBehaviour
    7. {
    8.     private Ray Ray;
    9.     private RaycastHit Hit;
    10.     private float moveSpeed = 10.0f;
    11.     private List<GameObject> selectedUnits = new List<GameObject>();
    12.     public NavMeshAgent unitAgent;
    13.     private Animator unitAnimation;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.        
    24.     }
    25.  
    26.     public void SelectUnit(Transform Unit)
    27.     {
    28.         DeselectUnit();
    29.         selectedUnits.Add(Unit.gameObject);
    30.         Unit.Find("SelectionIndicator").gameObject.SetActive(true);
    31.     }
    32.  
    33.     public void MultipleSelectUnit()
    34.     {
    35.  
    36.     }
    37.  
    38.     public void DeselectUnit()
    39.     {
    40.         foreach (GameObject unit in selectedUnits)
    41.         {
    42.             unit.transform.Find("SelectionIndicator").gameObject.SetActive(false);
    43.         }
    44.         selectedUnits.Clear();
    45.     }
    46.  
    47.     public void MoveUnit(Touch touch)
    48.     {
    49.         foreach (GameObject unit in selectedUnits)
    50.         {
    51.             unit.GetComponent<Animator>().SetInteger("toDo",6);
    52.             unitAgent.SetDestination(touch.position);
    53.         }
    54.     }
    55.    
    56. }
    57.  
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Well, how do you call this MoveUnit method?
    Also, does it work if you comment out the Animator set before the SetDestination? It's very possible the GetComponent call is throwing a NullReferenceException causing the entire method to simply stop there.

    For future reference, please only post the relevant code and include any error messages you might be getting.
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Have you tried to debug your MoveUnit, like printing out the touch position? First verify that it is valid position in the world and not some wrong coordinate (might be million reasons for that kind of mistake/error) and then check if the path exists and if the path is valid. If I remember correctly, NavMeshAgent.pathStatus gives the current status of the path (valid/invalid). If the path exists and is valid, then continue searching for the cause elsewhere.
     
  4. hazuhiro

    hazuhiro

    Joined:
    Feb 14, 2019
    Posts:
    34
    thanks guys, i already solve it, the problem is with my vector, i should not use touch,position, instead i should use hit.point