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. Dismiss Notice

Question animator and movement

Discussion in 'Animation' started by katanant, Jun 21, 2023.

  1. katanant

    katanant

    Joined:
    Jul 16, 2022
    Posts:
    10
    Hello, I have a problem, I'm doing several tests in unity, following some tutorials I created a character whose movement logics are in an empty parent object called player simply including the character controller, now ... everything works fine except one thing, i have an animation of vaulting over an obstacle...this animation runs just right and everything seems to be where it should be....the problem is that actually the character controller collider and all its elements don't follow the movement of the animation while remaining distant ... so I tried to do several tests ... through actions by checking the position with get position ... of each object and even of the animator and although visually it seems to move in reality the positions do not change, I apologize if I was not clear but I'm a total beginner and I hope I have made the idea...
     
  2. katanant

    katanant

    Joined:
    Jul 16, 2022
    Posts:
    10
    I post two videos, seeing what happens is worth a thousand explanations...



    in the first video the player walks, runs, jumps and rolls on the ground after jumping only and only after a run, and so far so good




    the problem arises here, i haven't added any script or fsm yet to control the vault animation but i started it by manually setting vault to true in the animator just to show what happens, as you can see the model moves but the collider and that is the character controller remains in the starting position causing many problems ...
     
  3. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    106
    You'll have to do 2 configurations:
    1. You're using "Bake into pose" in the "Root Transform Position" settings in your AnimationClip, meaning the moving would only be as a part of animation, but not real Transform moving of root.
    After uncheck the baking option, you would notice that the "Vampire" GameObject is Actually moving (if your Animator's "Apply Root Motion" is on).

    2. However, your real root is not at the "Vampire", but the "Player". So, you'll have to relay your position & rotation changes up to your "Player".
    Thankfully, it can be achieved by OnAnimatorMove() event function.
    Here's a sample script of relaying position change to root's Transform:
    Code (CSharp):
    1. // this component should be attached on the same GameObject of the Animator
    2. public class RootMotionRelay : MonoBehaviour
    3. {
    4.     [SerializeField] private Animator animator;
    5.     // your real root
    6.     [SerializeField] private Transform playerRootTrs;
    7.  
    8.     private void OnAnimatorMove()
    9.     {
    10.         // you have to adjust this moving part to your CharacterController version
    11.         playerRootTrs.position += animator.deltaPosition;
    12.         playerRootTrs.rotation = animator.deltaRotation * playerRootTrs.rotation;
    13.     }
    14. }
    You'll need to adjust this to your CharacterController version.
     
    katanant likes this.
  4. katanant

    katanant

    Joined:
    Jul 16, 2022
    Posts:
    10
    thank you very much, as soon as I can I try, I understood a little in theory but I didn't know how to apply it, thank you very much
     
  5. katanant

    katanant

    Joined:
    Jul 16, 2022
    Posts:
    10
    great, it works ... thank you very much:D
     
    Yuchen_Chang likes this.