Search Unity

How to Make NavMeshAgent Turn With RootMotion

Discussion in 'Navigation' started by uonlyliveonce, Sep 18, 2020.

  1. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    41
    I have a navmesh and blendtree setup (from that one official mecanim unity project that was deprecated on the asset store - the one with the Teddy Bears where you can click and the navmesh agent will move to that spot with the blendtree rootmotion animations).

    I'm trying to figure out how I can code "face this gameobject" with this set up. I considered setting the navmesh destination to be in the direction of the object I want the agent to face (but not far enough that the agent will actually move) and that should hopefully get the agent to use the rootmotion turn animation to turn in place to face the target gameobject but I wanted to ask here to see if there was a better way to accomplish this.

    The rootmotion is "Handled by script":


    void OnAnimatorMove()
    {
    agent.velocity = animator.deltaPosition / Time.deltaTime;
    transform.rotation = animator.rootRotation;
    }
     
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    Where the bot looks and where it moves do not have to be the same. If your bot character (animated model) is a child of the GameObject with the NavmeshAgent attached, you can rotate the animated model towards the place the AI must look. Then to animate the model, calculate the AI movement velocity relative to where the model is facing.

    If your bot is moving North but facing West (e.g. to shoot at the player) you will instead trigger a "sidestep" animation. The movement direction of the bot is +ve Z direction which would usually mean the walk forward animation. But relative to where the bot looks, the movement is +ve X direction so sidestep.