Root Motion finetune with LookRotation
I'm rotating a cube with Root Motion animations.
The player input.Axis controls are in a StateMachineBehaviour script on each idle pose in the Animator (6 sides of the cube, with each side a 0, 90, 180, 270 rotation makes 24 idle poses)
Here's one of those 24 scripts as an example:
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (Input.GetAxis("Vertical") > 0.1f) //up (w)
{
animator.SetBool("IsRotating", true);
animator.Play("Bottom0-Front0");
}
if (Input.GetAxis("Horizontal") > 0.1f) //left (a)
{
animator.SetBool("IsRotating", true);
animator.Play("Bottom0-RL0");
}
if (Input.GetAxis("Vertical") < -0.1f) //down (s)
{
animator.SetBool("IsRotating", true);
animator.Play("Bottom0-Back180");
}
if (Input.GetAxis("Horizontal") < -0.1f) //right (d)
{
animator.SetBool("IsRotating", true);
animator.Play("Bottom0-RR0");
}
}
The root motion works fine. The cube rotates on all sides. It also tumbles in the camera forward direction. (*see script below)
But when I rotate the camera 180 degrees around the cube during one of it's idle poses, the new forward applies to the direction of the cube (which is great), but it plays the animation is was supposed to play before I rotated my camera. In other words it is not using the 'right' animation. It actually seems like the Input.getAxis of the Statebehaviour already made it's choice of direction before the 'new' forward direction was decided outside the Statebehaviour? How can I fix this?
void FixedUpdate()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Vector3 cam = Camera.main.transform.forward;
cam.y = 0.0f;
if (h != 0 || v != 0)
{
transform.rotation = Quaternion.LookRotation(cam);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612215916im_/https://answers.unity.com/themes/thub/images/avi.jpg)