Search Unity

Pathfinding glitches rotation, then glides, then does nothing.

Discussion in 'Scripting' started by cscxfjug, Dec 8, 2018.

  1. cscxfjug

    cscxfjug

    Joined:
    Aug 6, 2018
    Posts:
    77
    I have followed Brackey's NavMesh tutorial diligently.
    Code (CSharp):
    1. https://www.youtube.com/watch?v=CHV1ymlw-P8
    2. https://www.youtube.com/watch?v=FkLJ45Pt-mY
    3. https://www.youtube.com/watch?v=blPglabGueM
    I have a LowMan, with following script attached:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityStandardAssets.Characters.ThirdPerson;
    6.  
    7. public class PlayerController_X : MonoBehaviour {
    8.     public Camera cam;
    9.     public NavMeshAgent agent;
    10.     public ThirdPersonCharacter character;
    11.  
    12.     void Start() {
    13.         agent.updateRotation = false;
    14.     }
    15.  
    16.     void Update() {
    17.         if (Input.GetMouseButtonDown(0)) {
    18.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    19.             RaycastHit hit;
    20.  
    21.             if (Physics.Raycast(ray, out hit)) {
    22.                 agent.SetDestination(hit.point);
    23.             }
    24.         }
    25.  
    26.  
    27.         if (agent.remainingDistance > agent.stoppingDistance) {
    28.             character.Move(agent.desiredVelocity, false, false);
    29.         }
    30.         else {
    31.             character.Move(Vector3.zero, false, false);
    32.         }
    33.     }
    34. }
    35.  
    I attached video recording showing the situation.

    There are no errors, and as I look at Brackey's Inspector it has same items as I do.
    When I press Play, my character gets idle, as they should.
    I press my mouse button, and the character walks.
    However, as the character gets to the target.
    It starts twitching and randomly turning in a single place.
    I edited variable "Stopping Distance" and set it to 1.
    The character stopped twitching at the end of the target, however
    when near target location, it starts slipping towards the target
    without animation. So I tried editing following:

    Code (CSharp):
    1.  
    2.         if (agent.remainingDistance > agent.stoppingDistance) {
    3.             character.Move(agent.desiredVelocity, false, false);
    4.         }
    5.         else {
    6.             agent.SetDestination(Vector3.zero);
    7.             character.Move(Vector3.zero, false, false);
    8.         }
    But that didn't fix it.
     

    Attached Files:

  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Removed "ADHD" from title. Stick to facts on dev forums - thanks.
     
  3. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Try to check if remaining distance is smaller than stopping distance, then use the "Pause" method (if I remember correctly) to pause/stop the agent. Surely it should not be needed to place those check on every frame in the main update function.