Help with Mathf.SmoothDamp on first person camera

Hello! I’m quite new to Unity and trying to use mathf.smoothdamp

I have a main menu camera that moves when moving the mouse over the screen for some added flare. I got that to work, however I thought it would look better if the motion was smooth. That’s what I’m having a problem with.

When I hit play, the camera is static and not moving with the new smoothing script.
I did not forget to set the public floats to numbers other than zero.
I did that before posting to the discussion forum.

using UnityEngine;

public class MenuCam : MonoBehaviour
{
    public float sensX;
    public float sensY;
    public float smoothspeed;

    //Folosit yRot in loc de yRotation pentru ca alt script foloseste yRotation si nu ma deranjeaza destul cat sa repar
    float yRot;
    float xRotation;

    float smoothposX;
    float smoothposY;
    public float velositiX;
    public float velositiY;

    private void Update()
    {
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

        float smoothposX = Mathf.SmoothDamp(yRot, mouseX, ref velositiX, smoothspeed * Time.deltaTime);
        float smoothposY = Mathf.SmoothDamp(xRotation, mouseY, ref velositiY, smoothspeed * Time.deltaTime);


        //De ce cacat e "+="
        yRot += smoothposY;
        xRotation -= smoothposX;
        //Blocheaza miscarea ca sa nu vada camera OOB
        xRotation = Mathf.Clamp(xRotation, 13.5f, 16f);
        yRot = Mathf.Clamp(yRot, 140f, 142.5f);

        transform.rotation = Quaternion.Euler(xRotation, yRot, 0);
    }
}

I apologise if this is a trivial issue, I’m not only a beginner to Unity, but also programming as a whole.

P.S. You can ignore the comments, they are unrelated to the issue. They don’t really describe anything useful.

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

If you need a simple way to move these positions smoothly, here’s some ideas:

Smoothing the change between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

Another approach would be to use a tweening package such as LeanTween, DOTween or iTween.