Search Unity

Question how to control root animation?

Discussion in 'Animation' started by Zimaell, May 20, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    There are many examples on the net, but they are related to the use of Rigidbody or CharacterController, I also did not quite understand what the animation itself should be, as a result, I had the main questions:

    Is it possible to control such an animation by simply changing the position?
    do i need to set the animation position or read the current state?
    (it's all about script control)

    I just don’t understand yet how it should be arranged, I set the root animation from mixamo (without binding) and visually it goes on, but after the cycle it returns to its place, while animator.deltaPosition is at zero, considering that I use it in OnAnimatorMove.

    tell me the order of execution of the root animation.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    I'm not sure I understand what you mean here, so I'll speak a bit broadly and feel free to clarify. In general you want to control the position of a gameobject. You do that either through the animation's root position, at which point the animation moves the gameobject itself, or by code (by setting the position directly). (or a combination). In general, if you care about movement feeling a certain way, you want to code your movement, so your characters move a certain way, and then you pass enough stuff to the animator controller, so visually the character is doing semi-plausible stuff.

    You can do it the other way around: have root motion in all your animations and have them control where your gmaeobject goes, but IMO, this is the road that leads to janky feeling movement and controls.
     
    Zimaell likes this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    At the moment, the controller works for me simply - I set the direction, move the object and, based on whether it moves or not, I set the animation
    Code (CSharp):
    1. DirMove.x = Input.GetAxis("Horizontal");
    2. DirMove.z = Input.GetAxis("Vertical");
    3. BaseObject.transform.position += new Vector3(
    4.     DirMove.x * SpeedCurrent * Time.fixedDeltaTime,
    5.     DirMove.y * 4f * Time.fixedDeltaTime,
    6.     DirMove.z * SpeedCurrent * Time.fixedDeltaTime
    7.     );
    8. if(DirMove == Vector3.zero){ AnimationRequire = "Idle"; }
    9. ....
    10. if(AnimationCurrent != AnimationRequire){
    11.     AnimationCurrent = AnimationRequire;
    12.     float fadeNormalizedTime = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
    13.     animator.CrossFadeInFixedTime(AnimationCurrent, 0.3f, 0, fadeNormalizedTime);
    14.     }
    everything works, but the gait looks unnatural, so I want to make a root movement.
    but I don't know how to do it...
    here is how in my example to rebuild it to the root movement?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Maybe someone else can chime in because I still want to tell you to do it the other way around and maybe it's getting counter productive at this point.

    (but just for completion: I would pass the speed of the movement to the animator and use that speed to speedup / slow down the walk / run animation so the gait seems reasonable at all speeds).
     
    Zimaell likes this.
  5. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    do you mean adjust animator.speed?
    tried it, it's not. i think i need to play the root animation on click and extract the resulting position, or i misunderstand...
     
  6. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    hmm, I don't know why, but this
    Code (CSharp):
    1. private void OnAnimatorMove(){
    2.         transform.position += animator.deltaPosition;
    3.         }
    didn’t work, but now it works, I don’t even know why and from what, before animator.deltaPosition gave zero, now it moves normally, mysticism...
    in general, the goal seems to have been achieved, although it is not clear why it did not work before ...