Add Camera Damping at Rotation and Position ..

Hey Guys,

since im working on several things,
i get back to my Camera Script,

actualy it works super good but i also would like to add kinda Damping effect but i cant realy figure out how,

maybe someone can help me quickly out,

cheers! :slight_smile:

    void CheckForRotation()
    {
        //GETS CONTROLLER INPUT
        float inputX = Input.GetAxis("RightStickHorizontal");
        float inputZ = Input.GetAxis("RightStickVertical");

        //MOUSE INPUT
        mouseX = Input.GetAxis("Mouse X");
        mouseY = Input.GetAxis("Mouse Y");

        //CONTROLLER + MOUSE INPUT (IN CASE SOMEONE USES CONTROLLER)
        finalInputX = inputX + mouseX;
        finalInputZ = inputZ + mouseY;

        //GETS THE Y AND X ROTATION TIMES THE SENSIVITY AND THE TIME(so it look good)
        rotY += finalInputX * inputSensitivity * Time.deltaTime;
        rotX += finalInputZ * inputSensitivity * Time.deltaTime;

        //RETURNS X (Clamping Works also while turning)
        rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

        rotY = ClampAngle(rotY, -(CameraFollowObj.transform.localEulerAngles.y + clampAngle), CameraFollowObj.transform.localEulerAngles.y + clampAngle);

        Quaternion localRotation = Quaternion.Euler(rotX, rotY + PlayerObj.transform.eulerAngles.y, 0.0f);
        transform.localRotation = localRotation;
    }

    void CameraUpdater()
    {
        // set the target object to follow
        Transform target = CameraFollowObj.transform;
        //move towards the game object that is the target
        float step = cameraMoveSpeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }

    public float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F) { angle += 360F; }
        if (angle > 360F) { angle -= 360F; }
        return Mathf.Clamp(angle, min, max);
    }
1 Like

One way is to use Cinemachine. :slight_smile:

Another way using the scripts above is to change cameraMoveSpeed gradually downwards as you approach your target.

So you would leave cameraMoveSpeed alone and make a new local variable in CameraUpdater();

That variable would be initially cameraMoveSpeed.

Then you would compute the distance from target.position to transform.position and when it gets below a certain distance, you would begin to slow cameraMoveSpeed down to a miminum version of it.

For instance (I am just typing this, not compiling it):

// this is the presumed speed we will use
float tempSpeed = cameraMoveSpeed;

// until we get close
float distance = Vector3.Distance (target.position, transform.position);

float close = 2.0f;  // change as needed

if (distance < close)
{
  // now we need to adjust tempSpeed from its max down to its minimum slowly

  float slowestCameraSpeed = 0.5f;   // we'll slow to this value

  float alpha = distance / close;   // alpha will go from 1.0f to 0.0f

  // now we lerp to the slower speed as alpha goes from 1.0 to 0.0
  tempSpeed = Mathf.Lerp( slowestCameraSpeed, tempSpeed, alpha);
}

tempSpeed = tempSpeed * Time.deltaTime;

// now you can use tempSpeed in place of step with your original Vector3.MoveTowards() function.
1 Like

Thanks, i will try it out tomorrow for sure!

But isnt there another simple way from Unity like Vector3.SmoothDamp and so on?

have been trying to figure something out with them but not realy working out good

If you want simple, just use Cinemachine.

I like to understand what I’m doing because it helps my brain form new connections and understand more things in Unity and gamedev in general, so I’m happy to develop some step-by-step operations that I can understand today, as well as two years from now if I dig this code up again.

1 Like

Thats Perfect,

well i will try later on your suggestion, looks simple and the idea behind it is actualy nice! :slight_smile:

So for now my “CameraFollowing” does work with damping,

i did it a bit simplier as i found in the Unity.Doc a simple function,

    void CameraUpdater()
    {
        // set the target object to follow
        Transform target = CameraFollowObj.transform;
        //move towards the game object that is the target
        float step = cameraMoveSpeed * Time.deltaTime;
        //If Damping is "on", the Camera will Damp depending on the Smooth value
        if(useCameraFollowDamping){
            transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, followDamping);
            return;
        }
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }

so now i still need kind of “Damping” for my “Rotation”, if i get something, i will Update it here

cinemachine is the best

Well, its an Old Thread, i did fix it thanks to @Kurt-Dekker ,
step by step, would suggest it to anyone else :slight_smile:

i end up with Cinemachine after coding it myself since Cinemachine offers many more features out of the box :slight_smile:

1 Like

what cinema machine component did you use?
and does it work if camera is inside something like head?