unity-game-enginequaternions

How to make FromToRotation end with a desired up vector, similar to when using LookRotation?


Use case: Translating the motion-tracking-detected player's shoulder, elbow, and wrist position into an in-game avatar. Instead of taking each position as ground truth or using IK, I want the relative rotation of each joint which I can pass into Unity's "Target Rotation" (such that, the avatar will take the user's arm's shape if and only if there are no obstructions).

In this case, I have successfully used FromToRotation to translate the shoulder joint's rotation to the in-game avatar (currently, the elbow is in the desired position, but the joint is not always facing the right way). I still need to specify the shoulder joint's Z-axis rotation (which actually dictates the elbow joint's direction) based on the position of the wrist. The Z-axis rotation is along the axis of the arm itself, and dictates which way the elbow joint is pointing. So, ideally, I would've liked the first step to have 0 degrees of Z-axis rotation and specify "up" as the up vector, but in reality, the result does have Z axis rotation even though the arm is pointed in the correct direction.

IIUC, In LookRotation, the resulting quaternion will allow the resulting orientation's up to be the input up vector. I want the same for FromToRotation, where at the end of the rotation the up vector corresponds to the input up vector. That way, I can point the arm in the correct direction while specifying "up" as the up vector, and eliminate any Z-axis rotation component. Is there an algorithm or series of functions I can call to achieve the desired result?


Solution

  • This seems like a simple question with a simple answer and I'm not sure why no one answered. I eventually was able to figure it out myself. Multiply the resulting FromToRotation quaternion with Vector3.forward. Then, call LookRotation(resultingVector, Vector3.up)