Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems trying to set the velocity of a NavMeshAgent.

Discussion in 'Scripting' started by Hotpots, Mar 14, 2017.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey,

    The current problem I have is that my player is running on the spot, although it is rotating in the correct direction based on where I click on the baked NavMesh.

    I'm trying to set the velocity of my NavMeshAgent by (which is exactly the same method utilised in the Adventure Game Tutorial.):

    Code (CSharp):
    1.     private void OnAnimatorMove() // Player Movement is handled.
    2.     {
    3.         agent.velocity = animator.deltaPosition / Time.deltaTime; // Move the NavMeshAgent (player) at a rate determined by the animator. S = D / T. D = Distance between frames. T = Time between frames.
    4.     }
    However when I debug agent.velocity I get (0, 0, 0) in the debug console. Therefore, apparently the NavMeshAgent has no current velocity.

    I am left puzzled, does anyone have any idea what the problem could be?

    Any help is appreciated!
     
    Last edited: Mar 14, 2017
    yfli likes this.
  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I still can't find a solution :(
     
  3. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Here is a .gif I captured of my problem: https://gyazo.com/4d0e98a19851dbef90cb681e2b57f122

    As you can see my avatar is clearly running on the spot, and I'm sure that is due to the fact the velocity of the navmeshagent is outputting 0. And I am unsure why this is happening when I am getting the position of the avatar in the last frame and dividing it by Time.deltaTime. So S = D / T.

    Still really stuck and its frustrating :(
     
  4. T-Zee

    T-Zee

    Joined:
    Oct 23, 2017
    Posts:
    31
    Did you ever find out how to solve this?
     
  5. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Hmmmm what I do for my NPCs is disable the rotation and movement of the nav agent. I then feed the desiredVelocity and desiredAngle of the navAgent to the animator, and then in OnAnimatorMove I feed the animation deltas to my rigidbody.velocity... It also might be worth making sure that the animation has root motion to begin with, and that apply root motion is enabled on the animator.