How would I keep my player from going past the background?

Can anyone tell me how I would code it to prevent my player from going past the x and y’s of the background (so he doesn’t go off the screen.) The camera doesn’t follow the player so I need a way to prevent my player from going off the screen.

You can also use Mathf.Clamp to keep the x and y (or whatever plane you are using) position within bounds.

Add some Colliders at the sides of the camera that extend really far so the player hits them.

I generally like to put a public transform variable in my class, and then have code compare against that transform. That way if I want to move boundaries, no code will be rewritten.

public Transform farBoundary;
	
	
	void Update () 
	{
		Vector3 position = transform.position;	
		
		if(position.x > farBoundary.position.x)
			position.x = farBoundary.position.x;
		if(position.y > farBoundary.position.y)
			position.y = farBoundary.position.y;
		
		transform.position = position;
			
	}

Really simple- go on the player and search for X/Y move and rotation co-ords. Then there should be boxes which will let you lock some co-ordinates. Check X and Y.
Finished :slight_smile:

Add walls at the end of the level that are just out of sight of the camera, the player will still be stopped by them