1

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 Answer 1

0

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...

2
  • As written, GetOffsetFromAvatar returns the global rotation from b, to a. Or, the global rotation from the AvatarOffsetPoint to the PlayerHand. I don't know what a Target is because the question doesn't say, but if it's the parent of the new AvatarOffsetPoint (or equivalent), that makes sense to me that it would work. By the way, you can just do Target.rotation = _offset;
    – Ruzihm
    Commented Dec 7, 2021 at 23:36
  • For simplicity's sake, you can replace _offset * Quaternion.Identity with just _offset :)
    – Immersive
    Commented Dec 8, 2021 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.