Detect Edge of Screen

Hi all,

Using the following code I can keep my (Space Invaders Style) ship within -10,10



But how could I change this code so that the ship will simply stay within the screen and not move so far to the left or right that the ship moves off screen?

Another way to ask this question is, how do I detect the edge of the screen? For example if I had a bouncing ball bouncing all over the screen where the screen edges act as walls. I would need to create colliders of course but how could I position those colliders so that they are exactly at the screen edges?

Many thanks


Well, i guess you talk about a 2D (or 2.5D) game? The best most general solution (it doesn't matter which camera mode you're using) is to calculate the "intersection" of the viewing frustum with your moving plane. This code suggests that you move your player along the global x-axis and the global z-axis matches the camera z-axis.

// UnityScript
var dist = (transform.position - Camera.main.transform.position).z;
var leftBorder = Camera.main.ViewportToWorldPoint(Vector3(0, 0, dist)).x;
var rightBorder = Camera.main.ViewportToWorldPoint(Vector3(1, 0, dist)).x;

transform.position.x = Mathf.Clamp(transform.position.x, leftBorder, rightBorder);

But keep in mind that will clamp the players pivot to the borders, so half of your player will leave the screen. You can modify the border values and add/subtract the half of your players size.

I know this is an old post, but I thought I’d post an answer anyways in case someone googles their way to this page.

The Mathf.Clamp is a nice function, but in cases where objects have a rigidbody with velocity, the Clamp makes the object flicker and glitch, since it’s only manipulating the objects transform, and not the rigidbody properties.

For example, if I want a bunch of balls to bounce around on within my phone’s screen, I wrote the following code, which works pretty nice:

// On each border, reverse an object's velocity as long as it's not on its way back in.
		if((transform.position.x <= leftBorder) && rigidbody.velocity.x < 0f){
			rigidbody.velocity = new Vector3(-rigidbody.velocity.x, rigidbody.velocity.y, 0);
		if((transform.position.x >= rightBorder) && rigidbody.velocity.x > 0){
			rigidbody.velocity = new Vector3(-rigidbody.velocity.x, rigidbody.velocity.y, 0);
		if((transform.position.y <= bottomBorder) && rigidbody.velocity.y < 0){
			rigidbody.velocity = new Vector3(rigidbody.velocity.x, -rigidbody.velocity.y, 0);
		if((transform.position.y >= topBorder) && rigidbody.velocity.y > 0){
			rigidbody.velocity = new Vector3(rigidbody.velocity.x, -rigidbody.velocity.y, 0);

You’ll have to define the screen’s borders as described in the other answer, and it’s the same principal for the topBorder and the bottomBorder.

Hope this serves as a good alternative for some people :slight_smile:

@Bunny83 your answer helped greatly. Thx very much. Now if anyone is interested in calculating the top and bottom boundaries, you can do the following:

float Bottom = cam.ViewportToWorldPoint(new Vector3(0, 0, dist)).y;

float Top = cam.ViewportToWorldPoint(new Vector3(0, 1, dist)).y;

transform.position.y = Mathf.Clamp(transform.position.y,Top,Bottom);

It solved my issue If you want to detect camera corners or edges in 2D May be this video will be helpful for you :- Camera Edge Collision In Unity3D C# - YouTube