c#: Mathf.Clamp causing unwanted behavior

Hello I am trying to make my character move in a specified area and no further so that they would stay in the view of the camera. The things I think to know about the game I’m trying to make, so that it might help us better solve this, is that the playercharacter is childed to an empty game object that moves forward and the playercharacter is free to move anywhere under that forward-moving-empty-game object-parent.

The specific codes I am trying to use to limit the player from just moving out of sight is this:

private float minX = -15.0f;
	private float maxX = 15.0f;
	private float minZ = 0.0f;
	private float maxZ = 20.0f;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0, 
			                     Mathf.Clamp(transform.position.z, minZ, maxZ));

And when I try implementing this code into my playerMovement Script the playercharacter cannot move further than the input float variables as expected, but unexpectedly will be stopped by an invisible force that I cannot imagine why. The player can still move left or right but cannot move forward and is pushed back to what appears to be the value where I set the player could move furthest back. If someone could give me advice/help here that would be much appreciated. Here is the code:

public class PlayerMovement : MonoBehaviour 
{

	public float movementSpeed = 10.0f;
	public float angleChangeSpeed = 100.0f;

	public bool isDead = false;
	
	**private float minX = -15.0f;
	private float maxX = 15.0f;
	private float minZ = 0.0f;
	private float maxZ = 20.0f;**

	// Update is called once per frame
	void Update () 
	{
		if(!isDead)
		{
			**transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0, 
			                                 Mathf.Clamp(transform.position.z, minZ, maxZ));**
			// new code
			float horizontal = Input.GetAxis ("Horizontal");
			float vertical = Input.GetAxis ("Vertical");

			Vector3 direction = new Vector3 (horizontal, 0, vertical);
			Vector3 finalDirection = new Vector3 (horizontal, 0, 1.0f);


			transform.position += direction * movementSpeed * Time.deltaTime;

			transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (finalDirection), Mathf.Deg2Rad*angleChangeSpeed);
		}
	}

	void SetDead(bool dead)
	{
		Debug.Log ("SetDead was reached");
		isDead = dead;
	}
}

I copy-pasted your script, and it works just as intended: the object moves freely within the limits given, so it’s x will be between -15 and 15, and it’s z will be between 0 and 20.

If it’s not working for you (you can’t change the x or z value by pressing your input), you have probably set up the Horizontal or Vertical axes in the Input Manager wrong, or you have some other script interfering with this script.

Quoting: “playercharacter is childed to an empty game object that moves forward and the playercharacter is free to move anywhere under that forward-moving-empty-game object-parent.”

So, you want the player to be “leashed” to the moving object, like it’s a small dog being walked by a steady-paced human?

Use localPosition instead of position. It’s the distance from your parent.

You can experiment with this in the Inspector. Whenever you have a parent, the Inspector numbers it shows you in the Position slots are really for localPosition. So, you can hand-move the parent, then hand-change the player-child’s position (really localPosition) to get a feel.

Same thing goes for rotation (but if the moving parent never turns and always faces 000, you don’t have to worry.)