- Home /
How to set screen boundaries?
I have a game where the camera is stationary and player cannot leave the screen. How can I set up screen boundaries so that they adjust to the various different screen sizes of different devices (for example, an iPhone versus an iPad)? The code I have should work, I am just not sure what code to put in for the numbers that I have numbers hardcoded in to work for an iPhone screen.
void Update () { //When Players reaches desired (L/R)possition make him stop if (transform.position.x <= -2.8f) transform.position = new Vector3(-2.8f, transform.position.y, transform.position.z); else if (transform.position.x >=2.8f) transform.position = new Vector3(2.8f, transform.position.y, transform.position.z); if (transform.position.y <= -4) transform.position = new Vector3(transform.position.x, -4, transform.position.z); else if (transform.position.y >=5) transform.position = new Vector3(transform.position.x, 5, transform.position.z); }
Answer by MikeNewall · May 30, 2014 at 01:41 AM
Use Camera.ViewportToWorldPoint to calculate the boundaries.
private float minX, maxX, minY, maxY;
void Start(){
// If you want the min max values to update if the resolution changes
// set them in update else set them in Start
float camDistance = Vector3.Distance(transform.position, Camera.main.transform.position);
Vector2 bottomCorner = Camera.main.ViewportToWorldPoint(new Vector3(0,0, camDistance));
Vector2 topCorner = Camera.main.ViewportToWorldPoint(new Vector3(1,1, camDistance));
minX = bottomCorner.x;
maxX = topCorner.x;
minY = bottomCorner.y;
maxY = topCorner.y;
}
void Update(){
// Get current position
Vector3 pos = transform.position;
// Horizontal contraint
if(pos.x < minX) pos.x = minX;
if(pos.x > maxX) pos.x = maxX;
// vertical contraint
if(pos.y < minY) pos.y = minY;
if(pos.y > maxY) pos.y = maxY;
// Update position
transform.position = pos;
}
This worked great! Only thing is there was one mistake in your code.. on line 27 it should be if(pos.y > maxY) not maxX. Once I fixed that though it worked perfectly. Thanks!
No worries. Didn't spot that. It's late I need sleep! :p I've fixed my answer for anyone one else who this might help.
Thanks!
Also note if you want to keep the edge of the player in the screen, rather than the center of the player (so the player is always fully visible), you could make these changes. Note this code requires that the player have a circle collider, but it could be altered because there are many ways to offset the values based on player size.
private float playerRadius;
//in Start
CircleCollider2D playerCollider = GetComponent<CircleCollider2D>();
playerRadius = playerCollider.bounds.extents.x;
//replace your code with this
$$anonymous$$X = bottomCorner.x + playerRadius;
maxX = topCorner.x - playerRadius;
$$anonymous$$Y = bottomCorner.y + playerRadius;
maxY = topCorner.y - playerRadius;
Answer by Sisso · May 30, 2014 at 12:36 AM
You can use Camera.WorldToViewportPoint to know the your boundaries.
Viewport is defined from (0,0) to (1,1).
var min = Camera.main.WorldToViewportPoint(0,0);
var max = Camera.main.WorldToViewportPoint(1,1);
Other options is to use Camera.ViewportPointToRay and Raycast in plane of player Plane