Hi forum
I have a transform with a rotation.
How can I create a Vector3 that is rotated in parallel with that rotation?
Carl Emil
Hi forum
I have a transform with a rotation.
How can I create a Vector3 that is rotated in parallel with that rotation?
Carl Emil
I think you want to simple multiply: tansform.rotation * Vector3.forward
There is also the more convenient transform.forward
I am not quite sure, what you mean by “parallel”
If you simply mean to apply the rotation to the vector, StarManta is correct, that you just need to multiply the quaternion representing the rotation with the vector, e.g.
rotatedvector = rotation * someVector;
you can also see the second script example on this page:
http://unity3d.com/Documentation/ScriptReference/Quaternion-operator_multiply.html
If the rotation is actually stored in a transform, you can simply use
rotatedvector = transform.TransformDirection( someVector );
as illustrated here:
http://unity3d.com/Documentation/ScriptReference/Transform.TransformDirection.html
If on the other hand, by parallel you mean how to get a vector, that is parallel with the axis of the rotation, it gets a little more complicated. I think what you would then be looking for is called an EigenVector of the rotation, but I am not going to look into that, unless you need it.
I assume that you want to take a vector, ignore its current direction and rotate it so it points in the same direction as the rotation vector. This means that you will be throwing away everything about the original vector except its length:
// assuming you have a rotation stored in "the_rotation"
// and any old vector in "the_vector"
var parallel_vector = the_rotation * Vector3.forward *
the_vector.magnitude;
Thus, the code above takes the forward pointing unit vector (Vector3.forward) and applies the rotation stored in the_rotation. This results in a vector pointing along the rotation direction of length 1. Then we just need to multiply it by the length of the original vector stored in the_vector
Again I’m overwhelmed by the quick and precise answers =)
Yes, I simply wanted to apply the rotation of a transform to a vector. I wouldn’t have guessed this:
Vector3 = Quaternion * Vector3;
It works to my satisfaction, but I have no idea what’s going on in the code. It doesn’t seem logic to me.
Thanks
Carl Emil
A Quaternion is a rotation. When you use the * operator in a script, in the case of Quaternion * Vector3, it rotates the Vector3 based on the Quaternion’s value.
That’s about as much as i can tell you before it gets into the territory of “it works. trust it.”
(PS: you can multiply a Q by another Q in this way if you need to combine the rotations.)
Thanks for the * tip
Choosing Unity in the first place was to make my production simpler, so here I am. I’ve looked at quaternion math before and It wasn’t love at first sight. I think I’ll go with the “it works. trust it.”-attitude in this case.
Carl Emil
Uph! Now I wan’t to go the other way: rotate a Quaternion (not connected to a transform) with a Vector3 representing x-y-z euler angles.
I’m writing a branch structure and I want to be in control of the angle of the child branches in relation to angle of the parent branch.
var myChildRotation = new Quaternion(myParentTransform.rotation);
var myChildOffRotation = new Vector3(Random.Range(-maxAngle, maxAngle), Random.Range(-maxAngle, maxAngle), Random.Range(-maxAngle, maxAngle));
// guessing beyound this line //
myChildRotation.Rotate(myChildOffRotation);
var myChild = Instantiate(myPrefab, Vector3.zero, myChildRotation);
Well this doesn’t work
Carl Emil
What you’ll want to do in this case is two steps:
Works =)