Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders.

My player uses the Character Controller component to move and I wanted to stop its movement once it reaches the border.

For example, when my player reaches the very left side of the screen, I want the Character Controller to stop moving on the left side, but it can still keep going upwards, downwards, and to the right. My character uses this code:

charController.Move (((player.forward * joyDelta.z + player.right * joyDelta.x)) * player.speed * moveSpeed * Time.deltaTime);

When I try this, however:

if(GameObject.Find("Player").transform.position.z > -100 && GameObject.Find("Player").transform.position.z < 100)
	{
		charController.Move (((player.forward * joyDelta.z)) * player.speed * moveSpeed * Time.deltaTime);
	}

if(GameObject.Find("Player").transform.position.x > -100 && GameObject.Find("Player").transform.position.x < 100)
	{
		charController.Move (((player.right * joyDelta.x)) * player.speed * moveSpeed * Time.deltaTime);
	}

When it reaches the very left side of the screen, it will stop going through the left, but it will also stop going through the right. The same goes for the ups and downs.

Is there a way I can do this?

Thanks! :slight_smile:

Create an empty game object and attach a collider to it then place it wherever your border is. Your player won’t be able to move past the object.