I’ve been working on the ability to rotate my camera around an arbitrary point based on mouse movement and have it working great except when it crosses the top/bottom of an axis. The code for moving the camera is:
// Get mouse inputs
float horizontalSpeed = 1.0f;
float verticalSpeed = 1.0f;
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
Vector3 upDirection = Camera.main.transform.up * h;
Vector3 rightDirection = Camera.main.transform.right * v;
Vector3 totalDirection = upDirection + rightDirection;
// Set camera variables
Camera.main.transform.position = MathHelp.Vector3Help.RotateAroundAPoint(Camera.main.transform.position,
Vector3.zero,
Quaternion.Euler(totalDirection));
Camera.main.transform.LookAt(Vector3.zero);
Note I have to multiply the up direction by the horizontal axis, and vice versa. And the code for rotating around a point is:
public static Vector3 RotateAroundAPoint(Vector3 point, Vector3 pivot, Quaternion angle)
{
// Center the point around the origin
Vector3 finalPos = point - pivot;
// Rotate the point
finalPos = angle * finalPos;
// Move back in place
finalPos += pivot;
return finalPos;
}
Any thoughts?
My goal is to try and be able to rotate the camera in a sphere anywhere.