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.

Question Character Orientation (To Bake, or Not to Bake Into Pose... is that the question?)

Discussion in 'Animation' started by HeyBishop, Dec 10, 2020.

  1. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    221
    This sounds a lot like a very frequently asked question, but I can't seem to wrap my head around it...
    I have a series of animation clips in the Animator Controller that play one after another. One of the animations is for the character to turn 180 degrees, next is them standing and looking for a bit, then another turning 180 degrees again. So, she should turn, look, turn again to the original orientation.

    Trouble is, when Root Transformation Rotation has Baked Into Pose enabled, after the animation clip is complete - it snaps back to its original orientation. If Baked Into Pose is disabled, the character won't rotate at all.
    Oh - and the animator's Apply Root Motion is "Handled by Script".

    So... what am I missing? How do I get my character to turn in place and stay facing that direction?

    Here's a video that might better explain my issue:




    I know this could be easier using Timeline, but it's so buggy I can't be bothered right now.
     
    Only4gamers likes this.
  2. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    221
    What I ended up doing was writing a behaviour to conduct the rotation, and I turned off the Bake Into Pose under Root Transform Rotation.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TransformBehaviour : StateMachineBehaviour
    4. {
    5.     [Tooltip("Time to begin this transform, normalized time")]public float startTransformTime;
    6.     [Tooltip("Time to finish this transform, normalized time")]public float exitTransformTime;
    7.     public Vector3 rotationTransform;
    8.     Quaternion startOrientation;
    9.     Quaternion finalOrientation;
    10.  
    11.     // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    12.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    13.     {
    14.         startOrientation = animator.gameObject.transform.rotation;
    15.         finalOrientation = animator.gameObject.transform.rotation * Quaternion.Euler(rotationTransform);
    16.     }
    17.  
    18.     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    19.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    20.     {
    21.         float adjustedTime = Mathf.InverseLerp(startTransformTime, exitTransformTime, stateInfo.normalizedTime);
    22.         animator.gameObject.transform.rotation = Quaternion.Lerp(startOrientation, finalOrientation, adjustedTime);
    23.     }
    24. }
    If the need comes up, one could add a position transform too.
     
    anycolourulike and Only4gamers like this.