• 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 Ezwrath · Jul 03, 2015 at 01:56 PM · camerascreenviewportscreentoworldpointviewporttoworldpoint

Viewport to World Coordinates

Hi guys, I've been looking for similar questions but I couldn't find what I was looking for.

Lets say I have a scene like t$$anonymous$$s. alt text

And I have two gameobjects, the left black dot being at x position -10 W$$anonymous$$le the right black dot being at X position 10.

I want the screen width to return approximately 40 in t$$anonymous$$s case. If i were to lower the screen width to here alt text

I would want the screen width to return approximately 20. I've seen the methods ViewportToWorldPoint and ScreenToWorldPoint but I don't t$$anonymous$$nk they can solve my problem here (i may be totally wrong). Any suggestion guys?

ex1.png (171.3 kB)
ex2.png (169.8 kB)
Comment
Add comment · Show 1
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 starikcetin · Jul 03, 2015 at 04:22 PM 0
Share

2 Replies

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

Answer by Ezwrath · Jul 03, 2015 at 10:13 PM

Someone else was able to answer on reddit To clarify I wanted the X coordinates for the left and rightmost visible pixels on the screen.

 //lowerLeft.x will be the X coordinate of the leftmost visible pixel
 Vector3 lowerLeft=camera.ViewportToWorldPoint(new Vector3(0,0,0));
 
 //lowerRight.x will be the X coordinate of the rightmost visible pixel
 Vector3 lowerRight=camera.ViewportToWorldPoint(new Vector3(1,0,0));
 
 float screenWidthInWorld=(lowerLeft-lowerRight).magnitude;

Only works for orthgrap$$anonymous$$c but that's what I have.

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 malkere · Jul 03, 2015 at 03:02 PM

you want to move your camera around so that the worldcoords visible on the blue plane are always between 20-40?

viewport dimensions are a flat float between 0 and 1 on the camera lens.

screenpoints are pixel based, 0-480, etc.

if you're orthograp$$anonymous$$c you could shoot a screenpointoray out from the left most point of the screen(in world coords) and see where in world coords it $$anonymous$$ts the blue plane, then do the same on the right (or not if you are always dead center) and adjust the FoV or use trigonometry to find the best location for the camera to be at (on the z axis)

if you're perspective... you can calc the distance to the blue plane, and knowing your FoV and distance to either of the black dots figure out how far you should actually be back using trigonometry and lerp towards the intended number.

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 Ezwrath · Jul 03, 2015 at 04:15 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

same visible area regardless of aspect ratio 8 Answers

Second Camera Position on Viewport to UI Element Position on Screen 2 Answers

Code behind 'Camera.ViewportPointToRay'? 2 Answers

ViewportToWorldPoint relative to camera angle 2 Answers

Having Problems using the camera viewport. 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