- Home /
Rotate object ON THE CAMERA UP AXIS
Hey.
I've created a rotation script in C# that will rotate a barrel according to the mouse.
I want the object to spin ON THE CAMERA UP AXIS.
Not the world's up axis.
Not the barrel's up axis. (It could be laying in any orientation.)
Not the barrel's parent's axis.
I want to rotate on the CAMERA UP AXIS.
Thanks!
Thanks :)
I tried that, but I wasn't entirely sure how to use it correctly.
EDIT: Code I tried.. Didn't quite work to what I wanted. It just moves left/right, up/down.
heldObject.transform.RotateAround(Vector3.zero, Vector3.up, -Input.GetAxis("$$anonymous$$ouse X") Time.deltaTime); heldObject.transform.RotateAround(Vector3.zero, Vector3.forward, -Input.GetAxis("$$anonymous$$ouse Y") Time.deltaTime);
Yes that will work. It just seems like overkill when you can just set the rotation directly.
By left and right, I mean in relation to the picture I have given.
I'm confused, have you not run my code? It clearly works in world space. I've triple checked it to be sure.
The x and y position of the mouse is normalized and turned into a rotation around two axes. When you assign to rotation the rotation is set in world space, regardless of the previous rotation.
Also any rotation is achievable by rotating on two axes.
However you're right that he did want it in camera space. For that, RotateAround is required.
I will submit another answer then, since the question has been modified. (I assume by you). By the way, before you deleted your answer he specified that he wanted to barrel to spin on the camera's Z axis with the vertical movement, and the barrel's Y axis with the mouse's horizontal movement. Both our answers were wrong.
Also the question originally asked how to rotate the barrel on two axes.
Answer by aldonaletto · Oct 06, 2012 at 11:04 PM
You could simply use Rotate - but specifying Space.World, so that the object would rotate around world axes:
function Update(){
transform.Rotate(0, Input.GetAxis("Mouse X") * speed, 0, Space.World);
}
where speed is roughly the turning speed in degrees per second - I said roughly because the value returned by axis "Mouse X" is a small value proportional to how much the mouse moved since last frame, which depends on mouse sensitivity.
Answer by rhys_vdw · Oct 07, 2012 at 11:33 AM
public class MouseRotate : MonoBehaviour {
[SerializeField] float m_rotateSpeed = 2f;
void Update() {
transform.RotateAround(Camera.main.transform.up,
Input.GetAxis("Mouse X") * m_rotateSpeed);
}
}