Quaternion offsets not consistent

Viewed 56

Current problem:

I'm using world space quaternion offsets to rotate a player model's hands to the player's true hands using:

private Quaternion GetOffsetFromAvatar(Quaternion a, Quaternion b)
{
     Quaternion _offset = a * Quaternion.Inverse(b);
     return _offset;
}

then applying it with:

public void OffsetTarget(Transform Target, Transform PlayerHand, Transform AvatarBone)
{
     /// OffsetTarget() is to be used to auto align avatar hands to player hands.

     Quaternion _offset = GetOffsetFromAvatar(PlayerHand.rotation, 
     AvatarBone.GetComponentInChildren<AvatarOffsetPoint>().transform.rotation);

Target.rotation = _offset * PlayerHand.rotation;
}

The expected result should allow players to change their model on the fly during runtime and the hands align correctly to the player's real hands, but when I test that, the hands face in completely different directions.

enter image description here.

1 Answers

Solution

In the OffsetTarget() method; I needed to change the last line were the offset is finally applied to the avatar.

From:

...
     Target.rotation = _offset * PlayerHand.rotation;
}

To:

...
     Target.rotation = _offset;
}

Someone smarter than me, correct me if I'm wrong about the reasoning behind this answer:

I was assuming I was doing the Quaternion math in world space, but the PlayerHand.rotation; part of the line was actually applying the math relative to the player's hand, instead of world space by using Quaternion.identity;.

I really need to stop asking a question just to answer it a few minutes later...

Related