Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by JeffAU · May 03, 2011 at 12:56 AM · positionscreenedge

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JeffAU · May 03, 2011 at 02:26 AM 0
Share

Absolutely Perfecto!!

Thanks so much for your assistance.

avatar image BobbleHead · Jun 30, 2011 at 07:25 PM 0
Share

I keep getting this error.. any ideas?

NullReferenceException cameraScript2.Update () (at Assets/Scripts/cameraScript2.js:7)

avatar image Setsuki · Jan 16, 2013 at 09:17 AM 0
Share

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.

avatar image arnoscene · Jan 12, 2015 at 01:52 PM 0
Share

@Bunny83 for great solution

avatar image mauleTTT · Jan 06, 2016 at 12:58 AM 0
Share

Thanks ! Your answer has helped me a lot !

Show more comments
avatar image
5

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 :-)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image popey · Jan 02, 2014 at 01:51 PM 0
Share

Super helpful, thank you.

avatar image
1

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);


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges