Hello, I am quite new to Unity, and I was wondering how to (in C# preferably) accomplish a simple full 360 degree in all directions rotation through quaternions (space flight simulation style)? Nothing fancy, no limitations, just being able to rotate in all directions, WITHOUT having the “input direction” change, meaning once I’ve passed 180, I don’t want the left and right to swap. So rotation relative to the world is what I’m looking for I THINK… Help is greatly appreciated! Thanks!
EDIT: To be more specific, I need full pitch roll and yaw on ALL 3 axis, the rotations will be added via mouse movement/joystick movement, roll will occur in place of yaw when a button is pressed, and become yaw again when the button is released.
If rotating only around axis Y (horizontal plane) and X (vertical plane), you can detect when the object is upside down and invert the horizontal control direction. If you plan to rotate around Z too, however, the resultant movement may become too weird to understand - it’s better to let Z alone, or use it just to bank in small angles.
// define the horizontal rotation direction:
float dir = 1;
if (transform.up.y < 0) dir = -1; // if upside down, invert horizontal dir
float dt = turnSpeed * Time.deltaTime;
// turn right/left (around Y) according to dir and Mouse X
angles.y = (angles.y + dir * Input.GetAxis("Mouse X") * dt) % 360;
// turn up/down according to Mouse Y
angles.x = (angles.x - Input.GetAxis("Mouse Y") * dt) % 360;
// assign new angles to eulerAngles (or localEulerAngles)
transform.eulerAngles = angles;
I don´t know if I understood your question correctly but…
You can use this to inform the angles in the 0 - 360 interval.
transform.rotation = Quaternion.Euler(x, y, z);
If you want it to rotate over time, just store your desired rotation into a new Quaternion and then do the RotateTowards(…)
Quaternion desiredRotation = Quaternion.Euler(0,180,0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRotation, rotationSpeed * Time.deltaTime);
Does it makes any sense?
These look good, but I need rotation on ALL 3 axis. It’s for a space-flight sim. type project, so turning is ALWAYS relative to the player. Thanks for the help so far!