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

NavMesh Agent, Root motion, Physics rb

Discussion in 'Navigation' started by Havie, Nov 4, 2020.

  1. Havie

    Havie

    Joined:
    Oct 12, 2019
    Posts:
    89
    Hello,

    I was wondering if anyone had any advice for making an AI character that uses:
    Root Motion Anims
    Nav-Mesh Agent
    Non-Kinematic RB


    Overview:
    I am trying to make a hacknslash game where the enemy AI engages in melee combat with nearby targets. Think Dynasty Warriors, Shadow of mordor, Assasins Creed, Devil May cry, skyrim etc. However I am having a hard time combining the root motion anims, to work w the rb and nav agent.

    I found this post:
    https://docs.unity3d.com/560/Documentation/Manual/nav-MixingComponents.html

    but found it slightly unhelpful.

    1)
    Code (CSharp):
    1.  _navAgent.nextPosition - this.transform.position
    is always the zero vector, so i cant seem to use this navmesh agent to predict my path, thats problem #1.

    2)
    Next this did not work at all:
    Use the difference between the simulated agent’s position (NavMeshAgent.nextPosition) and animation root (Animator.rootPosition) to calculate controls for the animations


    3) The article suggests the rb be kinematic. Is it possible for the rb to not be kinematic? I would like to make use of unitys physics, I am not sure how turning it to kinematic would allow me to simulate knockback collisions and gravity? Do I just have to manually code this onCollisionEnter / OnTriggerEnter ?

    4)
    My player character doesnt use a navmesh agent, hes a 3rd person controller using the same state machine/logic as the AI.
    Currently I am overriding OnAnimatorMove and moving the player via the rb:
    Code (CSharp):
    1. _rb.MovePosition(_rb.position + Animator.deltaPosition);
    then the AI is being moved via the navmesh/BuiltInRootMotion, its very gross:
    Code (CSharp):
    1. Soldier ai = (AISoldier)_Brain;
    2.                 if (ai._navAgent && StateMachine.CurrentState.GetType().Equals("MoveState"))
    3.                 {
    4.                     ai._navAgent.velocity = Animator.deltaPosition / Time.deltaTime;
    5.                     return;
    6.                 }
    7.   Animator.ApplyBuiltinRootMotion();
    I could very much use some advice ! please and ty
     
    Last edited: Nov 4, 2020
    BeorGames likes this.
  2. OneSketchyGuy

    OneSketchyGuy

    Joined:
    Oct 27, 2016
    Posts:
    14
    I can advice you, but I'm probably wrong and am open to being corrected.

    1) According to the article you've set this up wrong. You should be using the animators deltaPosition OR the navAgent's position, you can't be using both.
    Setting the next position tells the navAgent you want to move to that location, and if you are trying to use root-motion that's simply not going to work. Alternatively I would suggest (at least if you want to use the navAgent movement) just disable root motion and animating indepently. If you want to use the navAgent then you want to make sure it doesn't have any control over your motion and simply by hand code in the ability for it to follow a path using root motion.

    2) See 1

    3) I would suggest disabling the animator and ragdolling when hit to handle kock back, or do it the old fashioned way and animate it.

    4) See 1

    It looks like most of your problems, and again I could be wrong, are caused by you trying to use the physics system instead of the animation system, but wanting to rely on animations. You can't really do that, if you want real feeling moving you might just consider using IK with your characters and handling movement using physics. Or ditch the physics idea and use animation.
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    I don't have any experience dealing with this sort of thing, but:

    That's always going to be false because a Type is never equal to a string.

    Code (CSharp):
    1. StateMachine.CurrentState is MoveState
    Will tell you if it's a MoveState or any thing that inherits from MoveState.

    Code (CSharp):
    1. StateMachine.CurrentState.GetType() == typeof(MoveState)
    Will tell you if it's a MoveState (but not anything that inherits from it).

    You likely want the first one.
     
    Havie likes this.
  4. Havie

    Havie

    Joined:
    Oct 12, 2019
    Posts:
    89
    Thanks for the reply @flowe321

    Ah I used Animator.deltaPosition, instead of Animator.rootPosition. This might give me some different options going forward. But yeah it def suggests using both.
    upload_2020-11-3_19-59-16.png "
    3)
    As far as anims vs ragdoll goes,
    I am looking to do a combination of both.
    It seems this game I am taking inspiration from simply played an anim when launched in the air.
    I want to do something similar, play the falling anim and at the same time turn up the weight of the IK ragdoll rig and blend between ragdoll and the anim.
    disabling the animator is not really an issue as this falling anim itself can have Bake In X,Y,XZ all checked as needed. Its more so a problem of the rigidbody acting strange. If the navmesh agent is driving everything, I feel like applying a force to the rb becomes strange. Maybe I should disable the navmeshagent when knockbacked?


    If I ditch the physics idea and only use animations, how do I create collisions (walking into enemies, walls, up stairs), knockbacks , aerial combat without a rigidbody on?