I have empty gameobject, with attached script. Script should rotate child (named “main”) around parent’s X axis. The problem is, that parent rotated in world’s coordinates around Y, so parent’s X looks along world’s Z.
var firstBase = Vector3(-300,-5,0);
var tranRotQuaV1 = main.transform.localRotation;
var tranRotQuaV2 = Quaternion.FromToRotation(Vector3(-1,0,0),firstBase);
tranRotQua = tranRotQuaV1 * tranRotQuaV2;
main.transform.localRotation = tranRotQua;
But, instead of rotate main around local X, tranRotQua rotate main around world’s X.
And, as for now I’m pretty confused with quaternions, I don’t see a way to rotate a rotation, and not a transform with it.
It seems that the order is wrong - I think it should be tranRotQuaV2 * tranRotQuaV1, what’s equivalent to this:
var firstBase = Vector3(-300,-5,0);
var tranRotQuaV2 = Quaternion.FromToRotation(Vector3(-1,0,0),firstBase);
main.transform.localRotation *= tranRotQuaV2;
But all that this code does is to rotate main about its local Z axis by the angle between the vectors (-1,0,0) and (-300,-5,0) - a very small angle: 0.955 degrees! Is this what you actually want to do?
It seems like the end result is to look towards first base, and main.LookAt(firstBase) would do that. But:
Imagine directions such as “go 50 meters, turn right…” to get gas and groceries. The first set goes from your garage to the station. The next set goes from the station to the store. If the second set of directions starts from your garage, or from anywhere else, it won’t make any sense.
Line 4 takes an unrotated object and rotates in two steps. Step two (tranRotQuaV2) should be a rotation from your facing after step 1. For example, QuaV2 = FromToRot(main.forward, firstBase-main.position);. This stuff is tricky, which is why there’s a LookAt command.
Also, is firstBase the location of first base, or the distance from you? (if you are at 000 they are the same.) If it’s the location, use firstBase-main.position to get “the vector I want to look down.”
I don’t know how, but it’s working now. Seems that create new project and load my current project afterwards helps…
But, answering my question - to change the rotation axis useful to implement quaternion not to transform.rotation, but to transform.forward, transform.up, transform.right… it still gives a little shake at unwanted axis - something around -3.459382e-06 degrees at axis, where it must be 0, so use of Transform.LookAt() preferred.