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
0
Question by cstabile18 · May 30, 2014 at 12:30 AM · screenboundsscreentoworldpointboundary

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

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

2 Replies

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

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;
     }
Comment
Add comment · Show 3 · 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 cstabile18 · May 30, 2014 at 02:40 AM 0
Share

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!

avatar image MikeNewall · May 30, 2014 at 03:16 AM 0
Share

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.

avatar image jamesk5 · Jul 03, 2015 at 02:11 PM 0
Share

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;
avatar image
2

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

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

23 People are following this question.

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

Related Questions

Implementing Camera bounds proportionally to its ortographic size 0 Answers

iPad touch is offset! Unity2d. 2 Answers

Is there an easy way to get on-screen render size (bounds)? 5 Answers

Viewport to World Coordinates 2 Answers

collider2D.bounds.Contains not working properly 3 Answers


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