Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jfaghihi · Jun 16, 2017 at 08:15 PM · worldtoscreenpointviewporttoworldpoint

undrestanding WorldToScreenPoint and ViewportToWorldPoint

Hello,

I have created two cubes in the scene and then attached the following code to the main camera in the scene. The Cube 's position I have selected as the target for the camera which is (0,1.95,0). When I run the program, target is (273.6, 217.4, 6.8) pixels from the left is the output for the following lines: Vector3 screenPos = cam.WorldToScreenPoint(target.position); Debug.Log("target is " + screenPos + " pixels from the left");

and target world position (1.2, 3.5, -6.6) is the output for : Vector3 p = cam.ViewportToWorldPoint(target.position); Debug.Log("target world position " + p ); Non of the above outputs match the Cube (target) position which is (0,1.95,0). Can anyone by chance explain this to me? Thank you,

public Transform target; private Camera cam;

  void Start()
 {
     cam = GetComponent<Camera>();

 }

 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         //Transforms position from viewport space into world space
         Vector3 screenPos = cam.WorldToScreenPoint(target.position);
         Debug.Log("target is " + screenPos + " pixels from the left");
         //Transforms position from world space into screen space
         //which is defined in pixels
         //The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.
         Vector3 p = cam.ViewportToWorldPoint(target.position);
         Debug.Log("target world position " + p );

     }
     
 }
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 Vicarian · Jun 16, 2017 at 08:36 PM 0
Share

The easiest explanation is that in the Inspector, you define transform positions in Local space. Depending on your Hierarchy, the cube may or may not be at (0, 1.95, 0) in World space. Additionally, Viewport space isn't the same as screen space. Viewports are parts of a screen, which may or may not consume the entirety of a screen. Think of window docking in Windows 10, where you can dock a window to the right or left side of the screen and pick another window to dock to the other side. The windows thus have a viewport of 0, 0.5 and 0.5, 1, while on a 1080p monitor, they'd be 0, 960 and 960, 1920 in screen space. World space is the space projected from world coordinates, through the viewport, and then to the screen. It's a fairly complicated subject involving linear algebra and 3D math, which I'm not great at, but I think I regurgitated the basics correctly at least.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jfaghihi · Jun 16, 2017 at 08:38 PM

Many thanks for your help!

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

67 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 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 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

Relationship of WorldPoint, ScreenPoint, and ViewportPoint 0 Answers

WorldToViewportPoint and ViewportToWorldPoint math 2 Answers

Questions behind rationale of code,Why does my code work with ScreenToWorldPoint but not ViewportToWorldPoint? 1 Answer

Moving an NGUI component to overlay game object. 1 Answer

WorldToScreenPoint returning VERY high values 0 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