Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Issue with rotating character's spine bone in the late update

Discussion in 'Animation' started by modernator24, Mar 10, 2022.

  1. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    208
    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.

    Code (CSharp):
    1. private float m_SynchronizedNeckRotation; // This value came from network
    2.  
    3. void LateUpdate() {
    4.     Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
    5.  
    6.     m_SpineTransform.localRotation = Quaternion.Euler(
    7.         spineRotation.x,
    8.         spineRotation.y,
    9.         spineRotation.z - m_SynchronizedNeckRotation
    10.     );
    11. }
    This works but rotating its body is kind of lagging, unnatural. So I tried to interpolate the rotation like this:

    Code (CSharp):
    1. void LateUpdate() {
    2.     Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
    3.     m_CurrentSpineRotation = Mathf.Lerp(m_CurrentSpineRotation, m_SynchronizedNeckRotation, Time.deltaTime * 5);
    4.  
    5.     m_SpineTransform.localRotation = Quaternion.Euler(
    6.         spineRotation.x,
    7.         spineRotation.y,
    8.         spineRotation.z - m_CurrentSpineRotation
    9.     );
    10. }
    Running this code makes my character rotate his body backward, like 180 degrees. So I tried to use quaternion instead:

    Code (CSharp):
    1. void LateUpdate() {
    2.     Vector3 spineRotation = m_SpineTransform.localRotation.eulerAngles;
    3.  
    4.     Quaternion targetRotation = Quaternion.Euler(
    5.         spineRotation.x,
    6.         spineRotation.y,
    7.         spineRotation.z - m_SynchronizedNeckRotation
    8.     );
    9.    
    10.     m_SpineTransform.localRotation = Quaternion.Slerp(m_SpineTransform.localRotation, targetRotation, Time.deltaTime * 10);
    11. }
    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?