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
1
Question by vexe · Nov 23, 2013 at 07:11 AM · camerascreennguiworldtoscreenpointworldtoscreen

WorldToScreenPoint, isn't getting it right?

Not sure what's going on - but for some reason I can't get WorldToScreenPoint to get me the right on-screen coordinates.

 public void OnHover(bool isOver)
 {
     var screen = Camera.main.WorldToScreenPoint(background.cachedTransform.position);
     Debug.Log("Slot: " + screen);
     Debug.Log("Mouse: " + Input.mousePosition);
 }

See:

alt text

The background variable is a UISprite - with a TopLeft pivot point, so I should get the right coords.

alt text

I also tried using the UI camera (which is orthographic) with NGUITools.FindCameraForLayer(background.gameObject.layer); - Same results. I also tried localPosition.

Also tried this, just to see if I get a correct x coord - no avail:

 var bounds = NGUIMath.CalculateAbsoluteWidgetBounds(background.cachedTransform);
 Debug.Log("Bounds: " + Camera.main.WorldToScreenPoint(bounds.min));

Note that my screen is 1333x768 - Sometimes, if I hover over a slot that's over to the right, I get numbers like 1444 for the x on-screen coords - which is higher than my screen's overall width.

Note also that it's only the width that's not getting calculated right - the height is fine. I tried screen.x - Screen.width and Screen.width - screen.x both didn't give me logical results (they shouldn't but I was just experimenting)

What am I missing here, why am I not getting the right coords?

Thanks.

EDIT: What I'm trying to do, is get the distance between the topleft corner of the slot, and the point where I click on the slot. So I figured I could do:

 var screenPos = Camera.main.WorldToScreenPoint(background.transform.position);
 var dist = Vector2(Input.mousePosition.x - screenPos.x, screenPos.y - Input.mousePosition.y);

This used to work in the past for some reason - now it's not. Could it be a NGUI-related issue?

todo.png (464.9 kB)
todo.png (144.0 kB)
Comment
Add comment · Show 9
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 Fornoreason1000 · Nov 23, 2013 at 07:34 AM 0
Share

there is a small glitch in this function where the camera takes is size ratio from the x coordinate.

avatar image vexe · Nov 23, 2013 at 07:37 AM 0
Share

could you elaborate more? what can be done in this case?

avatar image Fornoreason1000 · Nov 23, 2013 at 09:10 AM 1
Share

the camera your using to display NGUI is camera.main yes? if not then you must use the correct camera. because the camera might be in a different position to the main camera. which will give the results you are having.

what i was talking about if you Convert between WorldToScreenPoint and ScreenTowWorldPoint you can get a strange offset on the x axis making everything out of whack, to fix it, try adding Camera.size by your screen quotient.

e.g I want an object at 0,0 on my screen, my camera is an orthgrapgic with a size of 100. my screen is 16:9 ratio when converting i end up with an x coordinate 170 units less than it should be becuase unity had subtracted the quotient.

avatar image vexe · Nov 23, 2013 at 09:16 AM 0
Share

"the camera your using to display NGUI is camera.main yes?" - No, when you create an NGUI UI it comes with its own camera. As I mentioned before I tried both the cameras - my main camera which has a perspective projection - and the NGUI camera which is orthographic, both gave the same results.

avatar image Fornoreason1000 · Nov 23, 2013 at 09:39 AM 0
Share

does your orthographic camera have normal rects? are they: X = 0, Y = 0, W = 1, H = 1 ?

Show more comments

1 Reply

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

Answer by ArkaneX · Nov 23, 2013 at 10:01 AM

I've no NGUI experience at all, but for me it looks like your main camera is positioned on the left of NGUI camera, or that it is rotated counterclockwise around y axis.

Could you post positions and rotations of both cameras, as well as background.cachedTransform.position from your example?

Comment
Add comment · Show 2 · 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 vexe · Nov 23, 2013 at 10:13 AM 0
Share

Dang! Yes, it seems like if I change the position/rotation of the main camera, stuff goes weird! (why?) - But then I wanted to double check calculating the thing from NGUI's camera - (which I could have sworn I done with NGUITools.FindCameraForLayer) - it worked out well.

avatar image ArkaneX · Nov 23, 2013 at 10:16 AM 0
Share

If you change position/rotation, then camera sees different parts of the scene. If you move it left, then things which were on the left just a moment before, can now be in the center or in the right, thus giving you different screen coordinates. The same about rotating.

This was not visible for you, because you observed NGUI camera results only. But the main camera was looking elsewhere.

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

18 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

Related Questions

WorldToScreenPoint returning VERY high values 0 Answers

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

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

Calculation behind camera.WorldToScreenPoint 2 Answers

how to use tk2d Sprite with NGUI Camera? 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