Make camera rotate around sphere in all directions
I am trying to create 3D view of a model, for that I need to rotate the camera around it left and right (which is easy) and also up and down (which is harder because I need to put limitations for that). I tried using Transform.RotateAround() and it works but gives unexpected results, when I reach x rotation limitation (can't go to much up, maximum 90 degrees view) the model rotates and the x position changes
I have also tried to do it myself with some math calculations, but it doesn't work good. These are my calculations (according to: ) This is what I did:
Transform mainCameraTransform = Camera.main.transform;
var radius = 10.0f;
var newZ = Mathf.Clamp(mainCameraTransform.position.z + Input.GetAxis("Mouse Y"), -radius + 1, 0);
var newX = Mathf.Clamp(mainCameraTransform.position.x + Input.GetAxis("Mouse X"), -radius + 1, radius - 1);
var newY = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(newZ, 2) - Mathf.Pow(newX, 2));
// y^2 = r^2 - x^2 - z^2
mainCameraTransform.position = new Vector3(newX, newY, newZ);
mainCameraTransform.LookAt(Vector3.zero);
Please help me, all I need to do is that the camera would rotate around an object and always look at it. The code above also generates an error when I get to the edges (newX+newZ > radius therefore newY is NaN). Do you know how can I do it?
Your answer
Follow this Question
Related Questions
Limit Rotation with RotateAround on 2 axis 0 Answers
Manual RotateAround 0 Answers
Maintaining Camera Rotation between modes 0 Answers
Why does RotateTowards rotate in an "orbit" rather than on the spot? Help me understand! 0 Answers
Limit Cam rotation 0 Answers