Search Unity

Question Having trouble following a path without NavMeshAgent

Discussion in 'Navigation' started by cubhead, Feb 4, 2024.

  1. cubhead

    cubhead

    Joined:
    Jul 11, 2021
    Posts:
    25
    I'm trying to make the agent follow a path without using NavMeshAgent, because I find it difficult to get it to move how I want. I've done it before, but for some reason this time I can't get it to work. There are no error messages, and based on Debug Logs the path seems to be calculated correctly, but the agent just won't move.

    Here is my script:
    Code (CSharp):
    1. private Rigidbody rb;
    2.  
    3.     [Header("Pathfinding")]
    4.     public float speed;
    5.     public Vector3 target;
    6.     public Transform leader;
    7.     private Vector3 targetPosition;
    8.  
    9.     private NavMeshPath path;
    10.     private int currWaypoint;
    11.     [SerializeField] private float newPathTime;
    12.     [SerializeField] private float minDistance;
    13.  
    14.     [Header("Ground Check")]
    15.     [SerializeField] private float rayLen;
    16.     private Vector3 groundNormal;
    17.     private LayerMask groundMask;
    18.     [SerializeField] private bool onGround;
    19.    
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody>();
    25.  
    26.         path = new NavMeshPath();
    27.         InvokeRepeating("GetPath", 0f, newPathTime);
    28.         currWaypoint = 0;
    29.  
    30.         leader = null;
    31.         targetPosition = transform.position;
    32.     }
    33.  
    34.     void GetPath()
    35.     {
    36.         NavMesh.CalculatePath(transform.position, targetPosition, NavMesh.AllAreas, path);
    37.         currWaypoint = 0;
    38.        
    39.     }
    40.  
    41.     void FixedUpdate()
    42.     {
    43.         if (leader == null) targetPosition = target;
    44.         else targetPosition = leader.position;
    45.  
    46.         groundCheck();
    47.         Move();
    48.     }
    49.  
    50.  
    51.     void Move()
    52.     {
    53.         if (!onGround) return;
    54.         int nextWaypoint = currWaypoint + 1;
    55.         Mathf.Clamp(nextWaypoint, 0, path.corners.Length - 1);
    56.  
    57.         rb.velocity = (path.corners[nextWaypoint] - transform.position).normalized * speed;
    58.      
    59.  
    60.  
    61.         if (Vector3.Distance(transform.position, path.corners[currWaypoint]) < minDistance) currWaypoint++;
    62.         if (currWaypoint > path.corners.Length) currWaypoint = path.corners.Length;
    63.     }
    64.  
    65.     void groundCheck()
    66.     {
    67.         RaycastHit hitInfo;
    68.         if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, rayLen, groundMask))
    69.         {
    70.             onGround = true;
    71.             groundNormal = hitInfo.normal;
    72.         }
    73.         else onGround = false;
    74.     }