Setting boundary for Z axis

Hi all, I have created a simple game which the player can move on it’s X and Z axis and right now I am facing problems in setting up my game boundaries, I was able to set the boundaries for my X axis but not for my Z axis.

This was what I wrote for my update function in C# script

void Update () {

	//boundaries for X
	float xPos = Mathf.Clamp (transform.position.x, -5,6);
	transform.position = new Vector3 (xPos, transform.position.y, 
	                                  transform.position.z);

            //boundaries for Z
	float zPos = Mathf.Clamp (transform.position.z, -14, -25);
	transform.position = new Vector3 (transform.position.z, transform.position.y,
	                                 zPos);
	//movement
		moveToPosition = transform.position;

	if (Input.GetKey (KeyCode.A)) {
					moveToPosition.x -= speed *Time.deltaTime;
			}
	if (Input.GetKey (KeyCode.D)) {
		moveToPosition.x += speed *Time.deltaTime;
	}
	if (Input.GetKey (KeyCode.W))
	    {
		moveToPosition.z += speed *Time.deltaTime;
	}
	if (Input.GetKey (KeyCode.S))
	{
		moveToPosition.z -= speed *Time.deltaTime;
	}
	transform.position = moveToPosition;

}

Its a typing error…Check the code.

Instead of transform.position.x you typed transform.position.z

//boundaries for Z
    float zPos = Mathf.Clamp (transform.position.z, -14, -25);
    transform.position = new Vector3 (transform.position.z, transform.position.y,
                                     zPos);

Chang the first parameter in new Vector3(transform.position.z to transform.position.x

//boundaries for Z
    float zPos = Mathf.Clamp (transform.position.z, -14, -25);
    transform.position = new Vector3 (transform.position.x, transform.position.y,
                                     zPos);

Hope this worked… :slight_smile: