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 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 this. alt text

And I have two gameobjects, the left black dot being at x position -10 While the right black dot being at X position 10.

I want the screen width to return approximately 40 in this 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 think 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

Well, since you can't edit what screen.width retuns, then you need to move either camera or dots or both of them to fit situation you want. screen.width will always return you a non-editable value, the width of game window.

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 orthgraphic 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 orthographic you could shoot a screenpointoray out from the left most point of the screen(in world coords) and see where in world coords it hits 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

Thank you for your input, I am using an Orthographic Camera. The 20-40 is just an example. But I want to return the width from the leftmost part of the screen, to the rightmost part of the screen in terms of world coordinates. The second image would return 20, because the transform.position.x starting on the leftmost part of the screen would be -10, and the transform.position.x on the rightmost part of the screen would be 10. Would I still have to shoot a screenpointoray?

Similar to finding the width of a 2D sprite in the scene by finding their leftmost starting point to their rightmost ending point. http://imgur.com/lychLjZ

Bounds houseBound = GameObject.Find("house4").GetComponent().bounds;

int width = houseBound.max.x - houseBound.$$anonymous$$.x; //width should be 3 -(-3) = 6

I would like to return the distance of the Right Part of the screen - the left part of the screen, in terms of world coordinates.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can the Camera be Positioned on the Game Screen through code? 0 Answers

Screen Boundary Solutions 0 Answers

ViewportToWorldPoint with multiple cameras 0 Answers

same visible area regardless of aspect ratio 8 Answers

Second Camera Position on Viewport to UI Element Position on Screen 2 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