Wrong Quaternion.FromToRotation

Hello, im working on a Character IK and want make the Head follow the Camera (or what ever position).
Everything works fine but i have problems with the rotation, when the character turns.

(The character need look down because the target is on the ground)
This is when the Y rotation of the character is on 0° (This is how it works correcly)

This is when the Y Rotation of the character is 90° (not correct)

This is when the Y Rotation of the character is 180° (not correct)

Im using Quaternion.FromToRotation and i think the first parameter is wrong…

void UpdateCameraIK(HitSystem_IK ik) {
        // get the position of the target to follow
        savedIKTargetPosition = IKTarget.position + IKOffset;

        // there is the bug in the "transform.TransformDirection(Vector3.forward)" i need to change this but have no idea how
        // the second parameter is just the distance vector of the target and the IK bones from my character
        Quaternion rotation = Quaternion.FromToRotation( transform.TransformDirection(Vector3.forward), (savedIKTargetPosition - ik.IKBone.transform.position).normalized);

        // apply the rotation to the IK Rotation quaternion and this IK Rotation quaternion will get multiply with the transform.rotation
        ik.HBone.IKRotation = rotation;
    }

Thank you for your help!

EDIT: I fixed it by inverse the direction of the distance vector and start with vector3.forward directly

Quaternion rotation = Quaternion.FromToRotation( Vector3.forward, transform.InverseTransformDirection(savedIKTargetPosition - ik.IKBone.transform.position).normalized);

Hey thanks for posting your solution, you helped me solve my own issue!

1 Like