0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ Can you explain in more detail or show an example of how the results of that first code differ from what you want? The better we can understand the problem, the better we can target solutions. \$\endgroup\$
    – DMGregory
    Commented Mar 10, 2022 at 11:26
  • \$\begingroup\$ @DMGregory Sure, I recorded a video to show all three attempts I tried: youtu.be/I7dsPEvaP4c \$\endgroup\$
    – modernator
    Commented Mar 10, 2022 at 14:12

1 Answer 1

0
\$\begingroup\$

@DMGregory had a pretty good answer on how to use Quaternion on unity a while back. It's still on the forums if you'd like to take a look. But basically, it's better to use RotateTowards instead of Slerp.

https://answers.unity.com/questions/717637/how-do-you-smoothly-transitionlerp-into-a-new-rota.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .