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

Cleanest way to stop character when performing a particular animation

Discussion in 'Animation' started by Entropia666, Oct 9, 2015.

  1. Entropia666

    Entropia666

    Joined:
    Sep 11, 2015
    Posts:
    6
    Hi all,
    I have some animations which require the character to be stopped.
    For example:
    • we have a Zombie arising for the ground and then seeking for the player; in the behaviour tree, it is simpler to directly set him in seek mode, but during the arise from the ground the enemy shouldn't move
    • Similarly, we would like the enemy to stop briefly when being hit
    So my questions are:
    • Is it a clean way to stop the movement of the character temporarily from the animator?
    • How can I do it? Should I use navmeshagent.stop() or rather navmeshagent.disable() ?
    All suggestions are welcome!
    Ciao,
    Michele
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Not sure about the first question Michele, but for the second, I think the best method is to have a short "got hit" animation when he gets hit. This should knock the enemy back a few paces or at least one.
     
  3. Entropia666

    Entropia666

    Joined:
    Sep 11, 2015
    Posts:
    6
    Thank you for your reply!
    Actually my problem is not about launching the animation "got hit", it's about the navmesh agent continuing to act and moving the character... I don't know if I'm being clear...
    Thanks!
    Michele
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    No you were clear, and I need to be clear I've not messed with the nav mesh as of yet, but I've watched the live training session a couple times, since U5 came out.
    I don't remember that being addressed in the live session, but if you used root motion on the animation and moved the character backwards - wouldn't that move him back? Might be that 1-1=0 so maybe you want the knock back to be extra exaggerated - knocking the enemy back twice as far as you'd expect him to. 1-2= -1. :)

    I'm sure this is totally incorrect, but if it's only for when the enemy is getting hit I'm guessing it might work.
     
    BrandyStarbrite likes this.
  5. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    first question is pretty simple check if an animations is playing and if it is set the speed and the nav mesh velocity to 0

    so
    if(Anim.GetCurrentAnimatorStateInfo(0).IsName("attack")){

    anim.SetFloat("Speed parameter for your blend tree if you got one ", speed);
    navmeshagent.velocity = Vector3.zero;


    }else {

    put your code here

    }

    this is just one way theres other ways to do it but using the velocity from the nav mesh agent and equaling it to a vector.zero will stop it from moving.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Entropia666:
    Hi, you could also try adding your stop navmesh agent commands to script of type StateMachineBehaviour, that is attached to zombie rise from ground state of your Animator.
    If you are familiar with them, you could try it, I guess stopping navmesh agent from OnStateUpdate method of StateMachineBehaviour script could be enough.
    When animator exits that state (rise from ground), it no longer updates that method, and allows your zombie to move once again.
     
  7. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    For the zombie rising from the ground scenario, you'd most likely want to disable the NavMeshAgent instead of using stop. NavMeshAgent.Stop() will cancel movement, but it wont halt NavMesh anchoring. So if you used Stop instead of NavMeshAgent.enabled = false the character won't crawl out of the ground, but will remain anchored to the nearest point on the NavMesh. Naturally, this isn't a problem if you aren't having any root motion applied during the "crawl out" animation.

    For the "got hit" scenario, using NavMeshAgent.Stop() is a good choice as it will continue to do necessary calculations but will halt all destination handling. That would be the cleanest way to handle that given the parameters above.

    Are you using a coroutine to handle the rising from the ground, or are you handling the timing of your stages by yourself?
     
    theANMATOR2b likes this.