- Home /
Detecting the Edge of the Screen
So I need a way to detect the edges of the screen regardless of it's formatting, for my Space Invaders style game. Or, is it possible to resize the screen so that the game is always the same size?
Also, I need a way to pause the game when you win (or lose), so that the player has time to see the text of "you win" or "you lose" before the application closes through Application.Quit();
That's two questions, you should ask the 2nd one separately
Answer by Waz · Jul 27, 2011 at 03:33 PM
To stop object at edge of screen:
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp01(viewPos.x);
viewPos.y = Mathf.Clamp01(viewPos.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
Note that as the screen resizes, so does the Camera, so you should only see you original issue if the aspect ratio changes.
When I do this it gives me 983, 421 (Camera.main.pixelWidth,Camera.main.pixelHeight) as the top right corner. When actually the top right corner (in the game view) is around 13, 6. What could be the problem here?
Ok so it seems to be working now but the sphere is still going halfway off the screen before stopping from the clamps. Any way to fix this?
Also, how can I check if the object is at the edges so that I can then begin to send it in the opposite direction?
The above will clamp the position, which is the pivot point, presumably at the centre of your sphere. Add the radius before converting to viewport space to fix this (bit not you'll then need to convert 4 points, one for each edge of the sphere).
If you want to bounce rather than clamp, you need something like:
if (viewPos.x < 0) { viewPos.x = 0; speed.x = 1.0; }
assu$$anonymous$$g speed is a variable holding the speed which you move the sphere by each frame.
Ok this works perfectly. Just one last thing - how do I position the sphere so that it starts in the upper right corner at the beginning of the game?
transform.position = new Vector3(viewPos.x, viewPos.y, 0); doesn't work because it just puts it at (~1, ~1, 0). Thanks a lot for your help!
Answer by DaveA · Jul 26, 2011 at 11:04 PM
What do you mean 'edges of the screen'? Do you mean the rendered window that the game runs in, or the physical device's screen? And what do you mean by 'detect'?
If I understand you at all, you may want to operate in Viewport space which has units from 0 to 1, always. See: http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToViewportPoint.html
By edges of the screen I mean the rendered window that the game runs in. Using the Viewport space gets me nowhere because I'm trying to get this sphere to bounce of the edges of the rendered window regardless of it's size. This Viewport space is giving me values between 0 and 1 so I can't say:
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
transform.position = new Vector3(viewPos.x, viewPos.y, 0);
because it puts the sphere at (1, 1, 0) ins$$anonymous$$d of at the top right corner like I want it to.
I need something to tell me what the dimensions of the rendered window is so that if you were to resize the window while playing it the sphere would still start at the corner.
Sorry about any confusion, I'm very new to Unity.
Your answer