Clamps won't work!

I have 2 problems. I am trying to create a endless runner type game and I decided I needed to clamp the distance the player can move up , down , left and right on the road, so I made some code for clamping the left and right sides of the road , but the player only seems to acknowledge the clamp on the left side , and I tried to clamp how much the player can go up and down the road but it doesn’t work. Please help.

Here is my code for the right and left clamp.

void Update () {
		
		//moves left and right along x axis
		transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")*movespeed);
		Vector3 tmpPos = transform.position;
		tmpPos.x = Mathf.Clamp(tmpPos.x, -8.0f, 9.0f);
		transform.position = tmpPos;	}
}

Code for Up and down clamp that won’t work.

void Update () {
		Vector3 tmpPos = transform.position;
		tmpPos.y = Mathf.Clamp(tmpPos.y, -5.0f, 5.0f);
		transform.position = tmpPos;	}

	}

You should merge the two scripts.

  1. Cache the vector3 of the transform position
  2. Check for input (left,right,up,down,…) and apply for cached vector3
  3. clamp cached vector3
  4. apply cached vector 3 to transform position