- Home /
Quaternion rotation issue
After importing a model from Blender I found out it had bones totally messed up, they're all pointing to weird directions, and therefore having wrong forwards. So what I'm trying to do, since I need to rotate them from script, is to apply some kind of rotation offset before the rotation itself. What I have so far is this:
transform.rotation = Quaternion.AngleAxis(y, Vector3.up) *
Quaternion.AngleAxis(z, Vector3.forward) *
Quaternion.AngleAxis(x, Vector3.right) *
Quaternion.LookRotation(target - transform.position);
Where x, y and z are offset degrees on 3 axes, and target is the point I'm trying to make my bone point at. After finding the right offset rotation it does indeed work, but the further away I move the object that has the bone attached, the more those offsets mess up, to the point they're not even close to poiting at the target.
I've tried using the transform right, up and forward instead of world axes, but this causes Gimbal Locks and crazy jittering at certain angles. I'm pretty sure I'm missing something on Quaternion rotations, but really I couldn't find anything for this problem.
Answer by Faradday · Mar 19, 2014 at 03:56 PM
I've managed to do it! In fact all I was trying to do was to rotate my bone towards a specific point, but instead of pointing its forward against it, i needed to have an arbitrary local direction vector pointing at it (its right, in fact). This is what I did, in case anyone needs it:
public static void RotateForwardToDirection(Transform transform, Vector3 forward, Vector3 direction)
{
Quaternion baseLook = Quaternion.LookRotation(direction.normalized, Vector3.up);
transform.rotation = baseLook;
Quaternion newLook = Quaternion.LookRotation(transform.TransformDirection(forward), transform.up);
transform.rotation = newLook;
}
Where forward is the arbitrary forward (passing Vector3.right will make the right component of the transform to point at the desired direction and so on).