Clamping and Damping

Hello Hello,

I have a little problem with my camera script. The thing is that I’ve set up a Clamp for the Camera, but that Clamp prevents the Camera from following the player smoothly. Without the clamp, the SmoothDamp works. I get why it is that way, but I am not sure how to solve it…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
   
    [SerializeField]
    GameObject player;

    [SerializeField]
    float timeOffset;

    [SerializeField]
    Vector2 posOffset;

    private Vector3 velocity;

    [SerializeField]
    float leftLimit;
    [SerializeField]
    float rightLimit;
    [SerializeField]
    float bottomLimit;
    [SerializeField]
    float topLimit;

    void Update()
    {
        Vector3 startPos = transform.position;
        Vector3 endPos = player.transform.position;

        endPos.x += posOffset.x;
        endPos.y += posOffset.y;
        endPos.z = -10;


        transform.position = Vector3.SmoothDamp(startPos, endPos, ref velocity, timeOffset);

        transform.position = new Vector3
            (
            Mathf.Clamp(player.transform.position.x, leftLimit, rightLimit),
            Mathf.Clamp(player.transform.position.y, bottomLimit, topLimit),
            transform.position.z
            );

    }
}

Clamp the endPos before you feed it to the SmoothDamp.

1 Like

Another way to solve it is to use Cinemachine from the Unity Package Manager… :slight_smile:

not sure what you mean by feed it it before, then it just clamps it :hushed:

cinemachine, yea, problem with THAT again is that it messes up my “brilliant” shaders really hard and solving that…no way :roll_eyes::sweat_smile:

What do you mean it clamps it? You want to feed the clamped value to SmoothDamp so that it moves towards the maximal target, you don’t want to clamp the SmoothDamp result otherwise it’ll overshoot and then get cut off like you’re experiencing now.

1 Like

To feed a value to a method means the same as passing a value to a method. You pass the endPos into the SmoothDamp method as the target position that the method shoud move towards. So what GroZZleR said is that you want to make sure you clamp your endPos variable before you even pass it to your SmoothDamp method.

I would recommend you change the type of your posOffset variable from Vector2 ro Vector3 and directly add (or subtract) the whole vector from your players position. That way you can get rid of you hardcoded magic number -10 and the code becomes cleaner. Also you can adjust the offset in all 3 axes in the inspector.

Assuming you’ve changed the type, you can simply do:

    void Update()
    {
        Vector3 startPos = transform.position;
        Vector3 endPos = player.transform.position + posOffset;
        endPos.x = Mathf.Clamp(endPos.x, leftLimit, rightLimit);
        endPos.y = Mathf.Clamp(endPos.y, bottomLimit, topLimit);
        transform.position = Vector3.SmoothDamp(startPos, endPos, ref velocity, timeOffset);
    }

Of course you would need to set the z component of your posOffset to -10

1 Like

That works indeed! Ony problem again is it messes up a reflection shader (that is probably not very professional but works), but I think based on your help I can figure something out :slight_smile:

Thanks!

Use LateUpdate for camera scripts. Not sure if this helps with your reflection but I generally recommend to do this. Making sure the Camera updates last, e.g. with the Script Execution Queue.

1 Like

I don’t get it. The code you’ve posted only changes the position of this object. The code changes we have suggested does affect this position the way you wanted. How exactly does the position of the object messes up what a shader does? Also if the changes mess up your shader, does that mean it works perfectly with your current code? That sounds strange and very unlikely. It’s more likely, like McDev02 said, that you may update your reflection camera too late?!? However if that’s the case it’s very unlikely that it currently works as it should.

The way we have shown you how to properly limit the movement is the only correct one. If you have issues with something related you may should include those details in your question. “Messes up” is not a proper description of a problem and not really helpful

Na, the reflection camera should update after the main camera updates which should update after all other related transforms (player) have been updated. We don’t know how the reflection camera works, code is needed here.