- Home /
Track Quaternion local Y axis to position
What do I need is to track Quaternion to some point using it's local Y axis, how do I think it should work:
Quaterion curQua; // Current and result rotation
Vector3 curPos; // Current position
Vector3 tarPos; // Target position
Quaternion tarQua = Quaternion.LookRotation(tarPos-curPos) // Target rotation in global space
Quaternion locQuaDelta = some black magic // Get tarQua in space of curQua
curQua *= Quaternion.Euler(0, locQuaDelta.eulerAngles.y, 0); // Result
How should I solve this problem?
Answer by lPVDl · Dec 04, 2015 at 10:08 PM
Solved this that way: Getting target vector, projecting it on object's local space, getting angle, than rotating object by that angle:
transform curTran; // Current transform
Vector3 tarPos; // Target position
Vector3 tarVect = curPos - tarPos; // Target rotation vector (z axis for object)
tarVect = Vector.ProjectOnPlane(tarVect, curTran.up); // Getting projection
float angle = Vector3.angle(curTran.forward, tarVect) // Rotation angle via local Y
if (Vector3.Cross(Transform.forward, targetVector).y < 0) // If vectors have reverse order
angle = -angle; // Set negative angle
curTran.rotation *= Quaternion.Euler(0, angle, 0) // Rotating.
Working for me, maybe there is some simpler way to do this, also, what will I do if I don't have transform?
Well, if you don't have transform data, you'll need to supply some data yourself.
The key to this is that, as you mentioned, you're looking for a rotation on a local Y-axis. This means that you either need transform data or you need to be able to provide any two non-parallel directional vectors in order to reconstruct axes through Cross Products. If your axes aren't global, then they need to be defined.
You've solved the problem in one of a handful of ways by using Transform data. Either way, though, you need at least one other vector to be able to deter$$anonymous$$e a relative angle. Otherwise, your only remaining reference points will be world-space axes.
Can I get all necessary data from Quaternion, local forward and local upward vectors?
As long as any two major axes can be defined, you can create a correlation between them.
That said, Quaternion rotations tend to be based purely on being relative only to eachother. Quaternion.Identity is the baseline of having no rotation applied, but once rotations are applied, Quaternion rotations are very hard to reverse in full and, similarly, challenging to analyze.
Ins$$anonymous$$d, Euler angles are the easy means of keeping track of rotational data, but lack the versatility of Quaternions in that it becomes important to maintain a proper correlation between rotations. As a result, you can run into problems involving Gimbal Lock, especially when facing directly up or down.
Now, with all of that in $$anonymous$$d, your basis of rotation will come from any two defined axes. As long as there's a generalized pair of axes and any one is accurate, all three can be properly derived from that and, therefore, a proper correlation between objects' current rotations on any given axis can be calculated.
For example, knowing the local Z-axis and having a second, diagonal line across the Y- and Z-axes allows you to attain an accurate X-axis, and then rebuild an accurate Y-axis from them.
As an example of this:
// Accurate
Vector3 localForward = new Vector3(1, 0, 0);
// Approximate
Vector3 localUp = new Vector3(1, 1, 0);
// Currently using a non-normalized vector, so normalizing the result
Vector3 accurateRight = Vector3.Cross(localUp, localForward).normalized;
// Both Vectors are normalized, so the result will be, too
Vector3 accurateUp = Vector3.Cross(localForward, accurateRight);
In short, you will get your best information by making use of the Transform data of the character(s) in order to obtain proper axis-alignment. Based on that, you're already handling everything correctly as-is. Flatten the new angle onto a pair of axes (local transform's "up"), then deter$$anonymous$$e the angle and orientation of that angle.
The only caveat in general is the need for enough information to deter$$anonymous$$e relative differences in your data, and that's the need for at least two axes. An angle doesn't care what orientation it's at -- it's simply the angle between two vectors. So any other information needed when obtaining an angle needs to be provided separately.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Child of a gameobject lookAt only around Y-axys 1 Answer
Odd rotation on the wrong axis 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
issue after 360 degrees rotation 1 Answer