- Home /
How can I rotate/orbit the camera around an object?
Hello new unity developer here trying to figure out how I can orbit/rotate the camera around my player object when the right mouse button is being held down (so hold right click, move mouse around to move the camera). So far I did manage to get things to work, however it's a bit scuffed when rotating it seems like the camera goes upsidedown or something so wanted to know how I can improve this.
public Transform focus;
public float sensitivity = 1;
public float speed = 30;
public float zoomLevel;
public float zoomPosition;
public bool rotateCamera = false;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Debug.Log("Right Mouse Button Clicked");
Cursor.visible = false;
rotateCamera = true;
}
if (Input.GetMouseButtonUp(1))
{
Debug.Log("Right Mouse Button Released");
Cursor.visible = true;
rotateCamera = false;
}
if (rotateCamera)
{
//Let the player drag the camera around (360 degrees/orbit idk)
//when they let go of right click camera will stay in the position they left it in.
transform.eulerAngles += 10 * new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
}
//Camera Zoom
zoomLevel += Input.mouseScrollDelta.y * sensitivity;
zoomLevel = Mathf.Clamp(zoomLevel, -30, 0);
zoomPosition = Mathf.MoveTowards(zoomPosition, zoomLevel, speed * Time.deltaTime);
transform.position = focus.position + (transform.forward * zoomPosition);
Answer by RehanSanish · Mar 12 at 01:41 AM
Instead of using a script, you can use the Cinemachine package. I think (i dont really know) that Cinemachine has what your asking for.
Oh I came across cinemachine when I was looking up how to make this, however, I decided that I would rather learn how to actually do the code myself since I've never coded camera stuff before.
Your answer
![](https://koobas.hobune.stream/wayback/20220613054106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scaled render texture misaligning mouse movement 0 Answers
3D mouse position 0 Answers
How to get Mouse input using the new Unity Input System 2 Answers
Unity Android Movement Problem 0 Answers
Detect Mouse in right side or left side For Player? 2 Answers