Issue with Chase Camera Limitations

Hi guys,

I wrote the below script to move the main camera towards the player incrementally.

float step = speed * Time.deltaTime;
Vector3 temp = new Vector3(transform.position.x, transform.position.y, target.transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, temp, step);

however i am unable to limit the max distance between the camera and the player.

any suggestions on how to do this? I want to make it so that when the camera reaches the player, the character dies.

Oh and my character is currently running along the z axis.

Thanks for your help!

Try this, dunno if work

float step = speed * Time.deltaTime;
Vector3 previousPosition = new Vector3(transform.position.x, transform.position.y, target.transform.position.z);
Vector3 tempPosition = Vector3.MoveTowards(transform.position, previousPosition , step);
transform.position = Vector3.ClampMagnitude(tempPosition, maxDistance);