Search Unity

Swinging character's arms using SetLocalBoneRotation()

Discussion in 'Animation' started by tigger, Sep 27, 2019.

  1. tigger

    tigger

    Joined:
    Jan 8, 2013
    Posts:
    95
    I'm trying to swing a character's arms using Animator.SetLocalBoneRotation() ( called during OnAnimatorIK() ). I'm thinking I need to somehow specify a rotation axis with respect to the player. For example, to swing the character's left upperArm, I'd want to rotate the bone about the character transform's left vector. I'm having a hard time getting my head around this. Anyone have any thoughts on how to approach this?
    Thank you.
     
  2. tigger

    tigger

    Joined:
    Jan 8, 2013
    Posts:
    95
    Well, I managed to get the arms to swing about an arbitrary, "global" axis but ONLY when an Animator isn't involved.

    Using the bone transforms returned by smRenderer, I can set the local rotation on those transforms.

    Once I've calculated my desired world rotation, I can get the corresponding localRotation for the bone using
    boneTransform.localRotation = Quaternion.Inverse(boneTransform.rotation) * worldRotation;

    When set into the boneTransform, everything looks perfect.

    However, when I try to do the same with the Animator and use Animator.SetLocalBoneRotation(), I get a completely different result (bone orientation is incorrect).
     
  3. tigger

    tigger

    Joined:
    Jan 8, 2013
    Posts:
    95
    I'm giving up on using Animator.SetLocalBoneRotation() to rotate bones. The on-screen results don't make sense (or, at least, aren't consistent with what you get when using "component bones" apart from Animator) and the rotation values (local and world) returned by GetBoneTransform() never change. Either something's broken in the API or I'm not using it right. I'm going to use LateUpdate() instead of OnAnimatorIK() and rely on "component bones". I know that works.