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 b1gry4n · Nov 03, 2014 at 03:25 PM · camerarectorthographicworldtoscreenpoint

[Solved]Orthographic camera RECT based off points on screen

Hello everyone. I dont know whether it is too early/late(no sleep) or I am completely clueless. I cannot figure out how to fix my orthographic camera's rect to match up to points on my main camera's screen. alt text

I have the screen coordinates of all of the blue points. I want to make my orthographic camera fit inside the smaller box. Anyone point me in the right direction for converting these 4 vector3 points into a rect I can use for a camera?

Explanation:

I have a UISprite in Ngui. From that sprite I am grabbing the 4 corners and converting those points to screen space using WorldToScreenPoint. I would like my orthographic camera to fit perfectly inside those 4 points. Thanks!

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

1 Reply

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

Answer by robertbu · Nov 03, 2014 at 04:02 PM

First you need just two points, not four. You need the lower left point (llPoint) and the upper right point (urPoint). Second, you need them in Viewport coordinates, not screen coordinates, so you want to use WorldToViewportPoint(). Given those to points, you can do:

  Camera.main.rect = Rect(llPoint.x, llPoint.y, urPoint.x - llPoint.x, urPoint.y - llPoint.y);
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 b1gry4n · Nov 03, 2014 at 05:34 PM 0
Share

close, but I needed to divide by the screen height/width as well for the width and height of the rect

avatar image robertbu · Nov 03, 2014 at 06:54 PM 0
Share

but I needed to divide by the screen height/width as well for the width and height of the rect

No, no, no. Viewport points go from (0,0) in the lower left of the screen to (1,1) in the upper right. So if you use WorldToViewportPoint() ins$$anonymous$$d of WorldToScreenPoint(), your calculations will be in the coordinate system used by the Camera.rect. No conversion is necessary. No division by screen width or height is necessary. You are going to a lot of unnecessary work. I'm assu$$anonymous$$g corners[2] is upper right;

 Vector3 llPoint = UICamera.mainCamera.WorldToViewportPoint(corners[0]);
 Vector3 urPoint = UICamera.mainCamera.WorldToViewportPoint(corners[2]);
 mapCam.rect = new Rect(llPoint.x, llPoint.y, urPoint.x - llPoint.x, urPoint.y - llPoint.y);

avatar image b1gry4n · Nov 03, 2014 at 08:21 PM 0
Share

you are correct, it looks much more clean as well. Thanks, marked as the answer

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

26 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 avatar image avatar image avatar image

Related Questions

How to make WorldToScreenPoint work with an orthographic camera? 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 0 Answers

Build for Windows : Camera rect always stretched 0 Answers

Changing Ortho Cam Size according to resolution 4 Answers

Keeping two objects in frame 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