I want to make my character's spine rotate to look up and down. It's animated, so I have to rotate in the LateUpdate, not Update. The only I need to do is just simply rotate the chest bone through the z-axis.
private float m_SynchronizedNeckRotation; // This value came from network
void LateUpdate() {
Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
m_SpineTransform.localRotation = Quaternion.Euler(
spineRotation.x,
spineRotation.y,
spineRotation.z - m_SynchronizedNeckRotation
);
}
This works but rotating its body is kind of lagging, unnatural. So I tried to interpolate the rotation like this:
void LateUpdate() {
Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
m_CurrentSpineRotation = Mathf.Lerp(m_CurrentSpineRotation, m_SynchronizedNeckRotation, Time.deltaTime * 5);
m_SpineTransform.localRotation = Quaternion.Euler(
spineRotation.x,
spineRotation.y,
spineRotation.z - m_CurrentSpineRotation
);
}
Running this code makes my character rotate his body backward, like 180 degrees. So I tried to use quaternion instead:
void LateUpdate() {
Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
Quaternion targetRotation = Quaternion.Euler(
spineRotation.x,
spineRotation.y,
spineRotation.z - m_SynchronizedNeckRotation
);
m_SpineTransform.localRotation = Quaternion.Slerp(m_SpineTransform.localRotation, targetRotation, Time.deltaTime * 10);
}
But from this code, now my character can barely rotate his chest. Also used SlerpUnclamped, but the result was the same.
I'm not sure why this won't work. Is there anything that I missed from this approach?