Search Unity

Mecanim with root motion can't be manually moved

Discussion in 'Animation' started by Fedora_Dev, Jun 22, 2019.

  1. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    I have a character that is controlled with root motion and mecanim. When the player runs into a trigger that would block them from moving further in that direction, I manually position the character at a new position a bit away from the trigger so they can't phase through it after the dialogue popup.

    Before i switched to mecanim and root motion, this setup worked just fine with a character controller. After adding root motion, however, the character will move to the desired position and then bounce back to where they started later on the same frame. I tried setting up a routine that essentially kept the player at the desired position until the soft pause was disabled by the dialogue and that worked except now the player just bounces back after the dialogue finishes.

    So to recap, Player hits trigger and moves to target location, then moves back. If I force the position each frame, they bounce between both points in a single frame.

    I have them setup as a character controller and animator on a single object. Character controller is used only for gravity calculations, so I am calling Move each fixed update. I can confirm that disabling this does not fix the issue.

    EDIT: Using the below code yielded the same result.
    Code (CSharp):
    1. animator.rootPosition = target.position
     
    Last edited: Jun 22, 2019
    radiantboy likes this.
  2. artaka

    artaka

    Joined:
    Feb 19, 2013
    Posts:
    128
    Have you tried disabling root motion, moving the transform, and then re-enabling root motion?
    Code (CSharp):
    1. animator.applyRootMotion = false;
    2. transform.position = target.position;
    3. animator.applyRootMotion = true;
     
    radiantboy likes this.
  3. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    Doesn't seem to make a difference. As a test, i tried turning root motion off and waiting a frame before setting the position, and then another frame before enabling root motion, but that didn't seem to fix this issue, either.
     
  4. artaka

    artaka

    Joined:
    Feb 19, 2013
    Posts:
    128
    ecv80 likes this.
  5. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    I wanted to use that method, however using `OnAnimatorMove` in the script completely overrides the base movement, so I would have to code gravity and collisions manually. It's a bit overkill just to implement something that should be incredibly simple.
     
  6. artaka

    artaka

    Joined:
    Feb 19, 2013
    Posts:
    128
    I think your issue may be somewhere else. I have a character setup with CharacterController and switch between root motion and script controlled movement without any issues. When I need the script to control the movement, I simply set animator.applyRootMotion = false and use CharacterController.Move to move my character. Otherwise, if need to control character with root motion I simply set animator.applyRootMotion = true and stop using CharacterController.Move.
     
  7. MineCode27

    MineCode27

    Joined:
    Dec 27, 2013
    Posts:
    9
    If you wish to do adjustment to your position when you have root motion enabled than you need to do it in late update.
     
  8. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    I added animator.applyRootMotion = false again, but instead of turning it back on in the same frame, i wait for the dialogue that pops up to end before turning it back on. Technically, this should've given the same result as having the coroutine set, but somehow it fixed the issue instead. I don't entirely understand why that change fixed it, though. I'll move on, I guess. Thanks for the help, everyone.