# How do I rotate my camera smoothly?

I have this code for rotating camera… now I wanna rotate it smoothly so when I drop my mouse , camera not to stop immediatly.

``````using System.Collections;
public class rotation : MonoBehaviour {

public float horizontalSpeed = 0.001F;
public float verticalSpeed = 0.001F;

// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0)){
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * (-Input.GetAxis("Mouse Y"));
h/=10;
v/=10;
transform.RotateAroundLocal(Vector3.forward,h);
transform.RotateAroundLocal(Vector3.forward,v);
}
}
}
``````

I don’t understand the goal from this code. First, RotateAroundLocal() is not documented. So you want to fix or change that. Most undocumented rotation functions take radians. You should change to using Transform.Rotate(). Second both line 14 and line 15 use ‘Vector3.forward’ as the axis of rotation. I’m assuming since you calculate both ‘h’ and ‘v’ that you want to rotate on two axes, but I’m not sure how you want that done. When you rotate on two local axes, your object motion will diverge from your mouse motion making it difficult for users, so you need to figure out how you want it to rotate.

Here is a script for you to play with. It takes your code and add some momentum so that the object continues to spin after the mouse is no longer moved. It rotates on two axes, so you can get an idea of the problems with rotating on two local axes. Put the script on a cube and play before adding it to a camera:

``````using System.Collections;
using UnityEngine;

public class Bug27 : MonoBehaviour {

public float horizontalSpeed = 1.1F;
public float verticalSpeed = 1.1F;
public float drag = 0.95f;

Vector3 v3Rotation = Vector3.zero;

private float h = 0.0f;
private float v = 0.0f;

void Update () {

if (Input.GetMouseButton(0)){
v3Rotation.z += horizontalSpeed * Input.GetAxis("Mouse X");
v3Rotation.x += verticalSpeed   * Input.GetAxis("Mouse Y");
}

v3Rotation *= drag;
transform.Rotate (v3Rotation);
}
}
``````

Note the drag solution here is very simple and not frame rate independent.