How to limit the distance of the target and the camera between a max

The script is attached to the camera.

var target:Transform;
private var z:float=0;
private var zoomSpeed:float=30;

function LateUpdate()
{
      z = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed *0.1;
      transform.Translate(Vector3(0,0,z),Space.Self);
}

I want to limit the distance of the target and the camera between a maximum and minimum.How to do this?

Hey,u can use (Mathf.Clamp,Mathf.Clamp 01 ) function where u can give min, max distance

  1. calculate the direction vector from the player to the camera by subtracting the vectors, i.e playerPosition-cameraPosition
  2. normalize the vector
  3. clamp the zoom between 0 and maxZoom
  4. multiply the normalized vektor with the (clamped) zoom