Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Spine rotation causes strange offset

Discussion in 'Animation' started by samson96, Apr 30, 2023.

  1. samson96


    Jan 8, 2020
    Hello everyone. I've run into a strange problem recently. I have a humanoid character with a simple animator with reloading animation. The character also has a script, that rotates the spine in root bone space in LateUpdate. Also, I have a debug sphere parented to the head bone, and before applying spine rotation I set the sphere to transform to some point in character space (p.s. by root and character space I mean the same thing - the root space of the character).

    So the expected behavior is this: debug sphere is not affected by the animation, and it's rotated by procedural spine modification.

    This works only if the spine rotation angle is 0, otherwise, the sphere sways a lot. I also tested it by rotating the spine bone in local space, but they had the same issue.

    Does anyone have an idea why it happens? I suspect it's the way Unity updates transform, but I'm not sure. Also, how can I achieve the desired behavior described above? I really appreciate any help.

    Video of the problem:

    Code (CSharp):
    1. public class SpineRotation : MonoBehaviour
    2. {
    3.     [Range(-90, 90)] public float angle = 0;
    4.     public Transform sphere;
    5.     public Transform spine;
    6.     public Vector3 offsetMS;
    8.     private Quaternion Rotate(Transform space, Transform bone, Quaternion rotation)
    9.     {
    10.         return space.rotation * (rotation * Quaternion.Inverse(space.rotation) * bone.rotation);
    11.     }
    13.     private void LateUpdate()
    14.     {
    15.         sphere.position = transform.TransformPoint(offsetMS);
    16.         sphere.rotation = transform.rotation;
    17.         spine.rotation = Rotate(transform, spine, Quaternion.Euler(angle, 0f, 0f));
    18.     }
    19. }