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 DevMerlin · Jun 03, 2013 at 07:42 PM · androidcamerascreenmathf

How do you clamp a camera by mobile width?

I need to prevent my camera from going beyond certain stage boundaries - however, I'm designing for Android and variable screen resolution. A basic Mathf.clamp won't do the job. I know I need an offset based on the screen width... perhaps offset = screen.width / 2? What is the best way to go about this?

Aspect Ratios

Okay.. figuring that a image of the issue may help. My mistake for phrasing it as resolution, but this isn't about placing GUI items - that's already taken care of.

The clamp is based on the center of the camera, local 0,0,0. X is being clamped, not the entire vector. If I base the clamp off a single ratio, then ratio 2 will see beyond the stage edge, thus the need to figure out the offset from "X".

ratioillustration.png (8.5 kB)
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
1

Answer by robertbu · Jun 03, 2013 at 08:01 PM

If the object is at a fixed plane distance from the camera, then you can use Viewport coordinates. Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right. So if the plane the objects move on is 10 units in front of the camera, you could convert do:

 var LowerLeft  = Camera.main.ViewportToWorldPoint(Vector3(0.0,0.0,10.0));
 var UpperRight = Camera.main.ViewportToWorldPoint(Vector3(1.0,1.0,10.0));

Now you have the maximum and minimum X and Y coordinates in world space that are visible at at a distance of 10 units in front of the camera. Note you may have to do more, because the calculations just say that a specific position is visible or not. Your objects have width. So you might end up using (0.1,0.1,10) and (0.9,0.9,10.0), or if things are really critical, using renderer.bounds to figure things out.

Comment
Add comment · Show 4 · 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 DevMerlin · Jun 03, 2013 at 08:36 PM 0
Share

How would that help me clamp the camera though? This is on a 2D plane - it's directly related to the last question I posted, but.. $$anonymous$$athf.clamp is a float. $$anonymous$$y goal is to devise a clamping method to prevent all screen resolutions for mobile devices from seeing "beyond" the stage of the game.

avatar image DevMerlin · Jun 03, 2013 at 09:29 PM 0
Share

Huh? Changing the field of view or orthographic size? You're missing it again. This is for dedicated Android devices. Yes, I'd change the aspect ratio to test the offset, but in play on an actual device, you can't do that. The point is to get the offset dynamically so the view can be clamped to the map edges. While this is - and I repeat - NOT a clone of the game, I'll point to Bad Piggies as an example, where you never see past the edge of the map no matter what device you are on.

avatar image robertbu · Jun 04, 2013 at 04:16 AM 0
Share

Reading again this is what I get. You have a "stage" which is bigger than is viewed by the camera, but has limits. The user can move the camera (or something track the action and move the camera), but you want to limit the camera so that the user can never see beyond the stage. Now I'm going to make three assumptions:

  1. The stage is at a fixed distance from the camera.

  2. You have the bounds in world space of the stage.

  3. You are looking down the 'Z' axis so that positive X is to the right and positive 'Y' is up.

So at start, you calculate the world coordinates of the lower left and upper right of the camera viewport (in world coordinates) as I've indicated above. Then you can calculate $$anonymous$$ and max values for the camera.

 var viewportWidth  = UpperRight.x - LowerLeft.x;
 var viewportHeitht = UpperRight.y - LowerLeft.y; 
 
 var $$anonymous$$X = stageLowerLeft.x + viewportWidth / 2.0;
 var maxX = stageUpperRight.x - viewportHidth / 2.0;
 var $$anonymous$$Y = stageLowerLeft.y + viewportHeight / 2.0;
 var maxY = stageUpperRight.y - viewportHeight / 2.0;

You can then use these values in a $$anonymous$$athf.Clamp() to make sure the camera never leaves the stage.

avatar image DevMerlin · Jun 04, 2013 at 04:33 AM 0
Share

I solved it, and with just two variables.

 leftOffset = 0 + ((Screen.width/22)*100)/215;
 rightOffset = 215 - ((Screen.width/22)*100)/215; 

This is dividing the viewport by the depth to get a percentage based on the total stage "length", then adding or subtracting to either side of the stage as needed. And it works.

avatar image
0

Answer by Owen-Reynolds · Jun 03, 2013 at 08:41 PM

It isn't so much screen resolution (pixels) as it is screen relative dimensions (Aspect Ratio.) If you make a game that fits on a screen 1.5 times as wide as it it tall (3:2 ratio) it will fit the same on a 300x200 screen, or a 3000x2000.

Exceptions are placing GUI-items using pixels. The trick is to always place and size them using screenPct (ViewPort coords, in robert's reply.) Lots of questions&answers here about doing that.

If you look at various non-Unity Android-phone stuff, that's what they're getting at. Instead of counting pixels, try to say stuff like "1/4 of the screen."

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

15 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

Related Questions

why my unity games run on android devieces,the screen will be wrong. 0 Answers

Detect if Android screen is widescreen? 2 Answers

Android - Camera orientation 1 Answer

How can I fix the screen disturbance of android 4.1/4.2 ? 0 Answers

(Android) Pick up objects with the cross from the centerof the screen 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