Limit Camera movement by distance from target

Hi guys,
A quick question…

I have a target and a Camera which moves back and forth (Vector3.forward) but I want to limit the movement with min and max distance from target. When distance from target to camera is 1000 it stops, when distance 6000 camera stops.

What I did so far is using Vector3.ClamMadnitude , but it clamps only maximum distance like radius.

How to restrict camera movement ?

// Move Camera
            cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
            transform.Translate(cameraMove * Time.deltaTime);

            // Find minimal camera position
            Vector3 minPosition = (transform.position - cameraRig.position).normalized * minZoomDistance;
            // Find maximal camera position
            Vector3 maxPosition = (transform.position - cameraRig.position).normalized * maxZoomDistance;
            // Find distance / radius where camera can go
            float movementRange = (maxPosition - minPosition).magnitude;
            // Calculate the distance of the new position from the center point
            Vector3 offset = transform.position - minPosition;
            // Clamp the length to the specified radius / distance
            transform.position = minPosition + Vector3.ClampMagnitude(offset, movementRange);

In this case, it is more convenient to implement your “ClampMagnitude” based on two conditions and a vector directed from the target to the camera:

using UnityEngine;

public class ClampDistance : MonoBehaviour
{
    public Transform target; // Target object
    public float minDistance = 5, maxDistance = 15;

    void Update()
    {
        // vector directed from the target to the camera
        Vector3 direction = transform.position - target.position;

        // Our ClampMagnitude
        if (direction.magnitude < minDistance)
        {
            direction = direction.normalized * minDistance;
        }
        if (direction.magnitude > maxDistance)
        {
            direction = direction.normalized * maxDistance;
        }

        transform.position = target.position + direction;
    }
}

You need to assign this script to the camera and then it will be between the minimum and maximum distance from the target.