I'm new to Unity in general, and particularly to animations, but what I'm trying to do should be fairly basic so I'm probably doing something simple wrong.
I've broken this down into a very simple demo case: I need to spin a cube smoothly.
Here's what I've done:
- Created a
TestCube
. - Using the
Timeline
window, created aDirector
component with aPlayable
. - Added an
Animation Track
to the timeline linked toTestCube
(which automatically added theAnimator
component to the cube).
I then recorded a very simple animation with two keyframes, where I rotate TestCube
180 degrees.
The result looks okay at first glance, but in reality it is actually quite jittery and uneven. I created a simple script to log how much the cube rotates each frame. In my real application, I'm mainly working in FixedUpdate
, but I'm interested in both, so I logged both.
using UnityEngine;
public class Test : MonoBehaviour
{
private Quaternion previousUpdateRotation;
private Quaternion previousFixedUpdateRotation;
private void Awake()
{
previousUpdateRotation = transform.rotation;
previousFixedUpdateRotation = transform.rotation;
}
private void Update()
{
Debug.Log($"Update|{Quaternion.Angle(previousUpdateRotation, transform.rotation)}");
previousUpdateRotation = transform.rotation;
}
private void FixedUpdate()
{
Debug.Log($"FixedUpdate|{Quaternion.Angle(previousFixedUpdateRotation, transform.rotation)}");
previousFixedUpdateRotation = transform.rotation;
}
}
I then graphed the output:
As you can see, the rotation of the cube is very much not smooth. My first thought way maybe the Animator.UpdateMode
parameter could help me. I set it to Animate Physics
and ran the test again, hoping to get a better result on FixedUpdate. But I got similar results.
Can anyone tell me what I'm doing wrong? How can I get a smooth response to match the smoothness of the animation curve?