- Home /
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.
Answer by Jamora · Aug 08, 2013 at 03:42 PM
You can also use Mathf.Clamp to keep the x and y (or whatever plane you are using) position within bounds.
Answer by lancer · Aug 08, 2013 at 03:39 PM
Add some Colliders at the sides of the camera that extend really far so the player hits them.
Answer by zombience · Aug 08, 2013 at 03:45 PM
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;
}
Answer by Marksmir · Aug 08, 2013 at 04:59 PM
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 :)
Answer by Banzai27 · Jan 30, 2020 at 10:23 AM
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