How to set a limit to where the camera can go?

I’m working on a game where you can only move up and down. The camera follows the player as they go up or down, but I didn’t want the player to be able to fly off into space, so I put a collider to limit him. But now the camera catches up, and the player looks like he can’t go past the middle of the screen because of the collider.

Here’s the script that moves the cam:

    private float moveSpeed = 2f;
    public Transform target; // Drop the player in the inspector of the camera


    void LateUpdate()
    {       
        Vector3 newPosition =
        new Vector3(transform.position.x, target.position.y - 3, transform.position.z);

        transform.position =
        Vector3.Lerp(transform.position, newPosition, moveSpeed * Time.unscaledDeltaTime);
    }

I’ve tried many things, and surprisingly, to no avail. How do I make it so that the camera can’t go past a certain point?

So it looks like target.position.y is the only part of the lerp target that changes. So I assume you want to ensure that the camera can’t go further along the y axis at some point. So just test to see if the target.position.y is more than the max or less than the minimum y position:

if(target.position.y > maxYPos){

cameraTargetPos = new Vector3(transform.position.x, maxYPos, transform.position.z);
}else if(target.position.y < minYPos){

cameraTargetPos = new Vector3(transform.position.x, minYPos, transform.position.z);
}

That should do it.

Cheers,