- Home /
Set (not offset) rotation on Vector3 axis?
Hello!
I'm building a controller with an arbitrary "down" direction, such that I can't use the Vector3 constants. (Vector3.down, Vector3.forward, etc)
I can rotate my camera just fine on the up / right axes, but I get a "roll" side to side on what would normally be the z axis.
This is how I'm setting rotation, with mainCamera being a cache of Camera.main, and the script being attached to the player object.
mainCamera.transform.Rotate(transform.up, totalPos.x);
mainCamera.transform.Rotate(transform.right, totalPos.y);
Any ideas for how to lock the forward direction to transform.forward? (Player controller transform)
Edit: Here's the full code
Vector2 mousePos = new Vector2(
Input.GetAxisRaw("Mouse X"),
Input.GetAxisRaw("Mouse Y")
);
if (lastMouse.HasValue)
{
Vector2 mouseDelta = lastMouse.Value - mousePos;
totalPos += new Vector2(-mouseDelta.x, mouseDelta.y) * lookSpeed;
mainCamera.transform.Rotate(transform.up, totalPos.x);
mainCamera.transform.Rotate(transform.right, totalPos.y);
}
lastMouse = mousePos;
Can you show the rest of your code, or at least where you are setting totalPos x and y. Depending on how you have it set up the solution may be as simple as switching those two lines of code you posted (if you are setting the overall rotation every frame, if your rotation is incremental then this won't work).
Added the full block to the original post.
Interesting, it looks like you are keeping track of an overall XY rotation vector (totalPos), but still applying a delta rotation each frame using transform.Rotate. In other words, I would expect you to be using transform.rotation = rather than transform.Rotate. Is your camera based upon the position of your mouse rather than the velocity of your mouse?
I'm curious, if you change just this line
mainCamera.transform.Rotate(transform.right, totalPos.y);
to
mainCamera.transform.Rotate(mainCamera.transform.right, totalPos.y);
then what happens?