I want to use a vector3 to define my Z rotation
How would I go about for example:
Vector3(0,1,0); a Z-rotation of 180
Vector3(0,-1,0); a Z-rotation of 0
Vector3(1,0,0); a Z-rotation of 90
Vector3(1,0,0); a Z-rotation of 270
etc…
I want to use a vector3 to define my Z rotation
How would I go about for example:
Vector3(0,1,0); a Z-rotation of 180
Vector3(0,-1,0); a Z-rotation of 0
Vector3(1,0,0); a Z-rotation of 90
Vector3(1,0,0); a Z-rotation of 270
etc…
You could use Euler angles - they are more intuitive, and can be easily converted to quaternions with Quaternion.Euler:
var Z180 = Vector3(0,0,180); var Z90 = Vector3(0,0,90); var Z270 = Vector3(0,0,270);
In order to not loose the initial object orientation, save its rotation at Start (it becomes the 0 reference) and combine it with the desired rotation:
private var initialRot: Quaternion; function Start(){ initialRot = transform.rotation; } // to rotate to the absolute Z270 rotation: transform.rotation = Quaternion.Euler(Z270) * initialRot; // to ADD the rotation Z90 to the current orientation: transform.rotation *= Quaternion.Euler(Z90);
NOTE: The latter does relative rotation (exactly like transform.Rotate), and accumulates small errors that progressively tilt the object to weird angles.
Thanks for all the previous answers they helped me answer this myself:
Because I wanted my objects right to face the direction I provided, and I only wanted a 2D rotation, I just literally changed its transform.right
and adjusted its transform.up
by making a cross product of its transform.forward
and transform.up
transform.right = direction;
transform.up = Vector3.Cross( transform.forward, transform.up );