Moving only in one direction?

Hello. I want my camera to follow the player’s z coordinate, but only in one direction? It is a platformer, so imagine that as the player is moving forward, the camera will stay behind them. But, if the player move’s backwards, the camera will not move (it doesn’t make sense out of context but it has a purpose). Here is what I have so far script(not anywhere near completion, I know), how can I modify it to do what I want? Thanks

function Update (){
    var Player   = GameObject.Find("Player");
    transform.position.z=Player.transform.position.z;
}

Without knowing specifically what you are doing its a bit hard to say but I’m guessing you’re making a kind of “chase cam” where the player can run away from the camera and it will follow but if the player tries to run back towards the camera it wont, thus preventing the player from backtracking.

If that’s the idea then what I would do is just check the distance between the camera and the player and if the distance is less than the desired distance, don’t update the Z.
I’m also not sure if you want the camera to move along the X and Y axis to follow the player, this code assumes that you do.
This code is completely untested:

function Update(){
	var player = GameObject.Find("Player");
	var oldPosition = transform.postion;
	var fixedDistance = 10.0f;
	
	//Get the distance between the player and the camera
	var distance = Vector3.Distance(player.transform.position, oldPosition);
	
	//Offset the camera from the player and have it follow
	transform.position = player.transform.position - (Vector3.forward * fixedDistance);
	
	//If the player is running in close to the camera, don't update Z position.
	if(distance <= fixedDistance)
	{
		transform.position.z = oldPosition.z;
	}
}