- Home /
how to make a simple full rotational quaternion for cameras
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.
Could you go into a bit more detail here? It's really not all that hard, but there's a few things you might be talking about here. For example, under what circumstances would the 'input direction change'? I don't quite follow what you're talking about there. That doesn't seem like something that would happen naturally.
Answer by aldonaletto · Nov 07, 2011 at 12:37 PM
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.
void Update(){ // 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; }
Answer by cj_coimbra · Nov 07, 2011 at 12:42 PM
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?
Answer by Drathro · Nov 07, 2011 at 06:24 PM
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!
If you want local rotations, use Rotate:
float sensitivity = 60f;
void Update(){ float dt = sensitivity Time.deltaTime; float dx = Input.GetAxis("$$anonymous$$ouse Y") dt; float dy = -Input.GetAxis("$$anonymous$$ouse X") dt; float dz = Input.GetAxis("Horizontal") dt; transform.Rotate(dx, dy, dz); } This works fine with a first person camera (childed to the ship). With a 3rd person camera, the controls may seem too complicated, and the axis inversion trick here will only make things worse.
Your answer
Follow this Question
Related Questions
Add rotation to rigidbody AddForce? 3 Answers
with quaternion how rotate around global Y ? 2 Answers
Off Screen direction indicator HUD element / wrestling with Quaternion 1 Answer
Stage selection problem 1 Answer
How do I use this current Quaternion to make GameObject Joints move Correctly? 0 Answers