Search code examples
c#unity-game-enginecamerasmoothing

How to make this camera movement smoother?


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;
    }
}

Solution

  • 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;