How to make this camera movement smoother?

Viewed 50

I am making a game where I would like the camera to be moved with a limit, I already have the script for that but I'd really like for the camera to be more smoother, If possible.

Thanks!

public class LimitedCamera : MonoBehaviour
{

    public float LimitAngleX = 10f;
    public float LimitAngleY = 10f;

    private float AngleX;
    private float AngleY;
    public void Update()
    {
        var angles = transform.localEulerAngles;

        var xAxis = Input.GetAxis("Mouse X");
        var yAxis = Input.GetAxis("Mouse Y");

        AngleX = Mathf.Clamp(AngleX - yAxis, -LimitAngleX, LimitAngleY);
        AngleY = Mathf.Clamp(AngleY + xAxis, -LimitAngleY, LimitAngleY);

        angles.x = AngleX;
        angles.y = AngleY;

        transform.localRotation = Quaternion.Euler(angles);

        transform.localEulerAngles = angles;
    }
}
2 Answers

In your LimitedCamera class:

public static float MouseSensitivityX = 0.1f;
public static float MouseSensitivityY = 0.1f;

And in your Update() method:

var xAxis = Input.GetAxis("Mouse X") * MouseSensitivityX;
var yAxis = Input.GetAxis("Mouse Y") * MouseSensitivityY;

You can also make sensitivity change in settings menu using PlayerPrefs.

the Solution is Multiply the Time.deltaTime in InputAxis for smoothing camera. This method inhibits frame fluctuations in the game and also makes it smoother on weaker systems.

var xAxis = Input.GetAxisRaw("Mouse X") * Time.unscaledDeltaTime * sensitivity;
var yAxis = Input.GetAxisRaw("Mouse Y") * Time.unscaledDeltaTime * sensitivity;

You can also control the speed by changing the sensitivity variable.

public float sensitivity = 10f;
Related