- Home /
Detect Edge of Screen
Hi all,
Using the following code I can keep my (Space Invaders Style) ship within -10,10
transform.position.x+=Input.GetAxis("Horizontal")*Time.deltaTime*speed;
transform.position.x=Mathf.Clamp(transform.position.x,-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
JeffAU
Answer by Bunny83 · May 03, 2011 at 01:36 AM
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 keep getting this error.. any ideas?
NullReferenceException cameraScript2.Update () (at Assets/Scripts/cameraScript2.js:7)
I was trying to generate a $$anonymous$$esh so I could use it as a collider so my character wouldn't go offscreen. I love your code so much.
Answer by Scopperloit · Jun 07, 2013 at 09:28 PM
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 :-)
Answer by kendyb · Jan 03, 2016 at 09:24 AM
@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);
Answer by aakashsolanki890 · Feb 19, 2021 at 04:58 PM
It solved my issue If you want to detect camera corners or edges in 2D May be this video will be helpful for you :- https://www.youtube.com/watch?v=-SlN5ekMLR8
Your answer
Follow this Question
Related Questions
Position GameObject to Bottom Left Corner 2 Answers
Anchor The Objects. 1 Answer
Cursor track object 1 Answer
World to Screen 1 Answer
click on screen to get the coordinate on the ground(x,z plane) 1 Answer