Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by identy15 · Mar 24, 2014 at 11:01 PM · worldtoscreenpoint

Issue with WorldToScreenPoint

Hi, i am trying to show some GUI item with a 3D object, let me first explain the scenario. There is a table with 3D items on it, and a camera from top down on the table so everything on the table can be seen from above, now what i do is

 screenPos = tblCam.camera.WorldToScreenPoint (selItemHandle.transform.position);

to convert the position from world to screen and then i draw the gui according to it

 leftButtonRect = new Rect(screenPos.x, Screen.height - screenPos.y, 48,48);
 GUI.Button (leftButtonRect, icnRotateLeft, myStyle)

It works fine but as i move the item from left or right, the GUI item get a bit off, if i move the item toward left the GUI item goes off toward right a bit, and the GUI item goes off toward left if i move the item toward right....

Can someone tell me how to fix this?

Thanks

Comment
Add comment · Show 4
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 robertbu · Mar 24, 2014 at 11:05 PM 0
Share

I'm not sure this is your problem, but your code anchors the upper left corner of your button to the world position, not the center of your button. Cannot tell where the anchor/pivot is in relation to the center on the selItemHandle object.

avatar image identy15 · Mar 24, 2014 at 11:11 PM 0
Share

Yup, the button top left position is exactly on the center of the gameobject, but when moved left or right it goes off from the center of the game object. I guess it might be because of the perspective view? if i change the camera to ortho view it works of, but in perspective view it goes off with movement

avatar image robertbu · Mar 24, 2014 at 11:15 PM 0
Share

If the object has depth, remember you are centering the position over the pivot point of the object, not the surface of the object. You would need to raycast or do some other calculation to figure out the surface position to align.

avatar image identy15 · Mar 25, 2014 at 08:58 AM 0
Share

I am now trying with raycast and with then do the worldtoscreenpoint on the hitobject point but still the same issue. how to get the surface position? And how can i get the width and height of an object and then convert it to screen width and height?

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by karma0413 · Oct 14, 2017 at 01:01 PM

I finally figured it out.... i was having a simiar problem....

Quick Description of my problem: I was using the Canvas to display a sprite ( a healthbar) which floats over all the characters. The problem however, was when i tried to obtain the WorldToScreenPoint it kept giving a result that was slightly off.... for example: the healthbar looked a little okay when the character was immediately in front of the camera... but as the character walks to the edge of the camera's fulstrum, the screens x,y placement becomes more and more incorrect.

Days and days of research and trying different combinations finally showed me that maybe there is a scaling issue which pointed me to look at Canvas / Canvas Scaler / scaling mode: scale with screen

Originally, this worked wonderfully when i had only 1 character and his healthbar stayed stuck to the top of the screen like old classic double dragon games. BUT when i made the decision to have many characters and they all need "floating healthbars", i didnt come back to re-evaluate whether this option needed to change.

Setting the canvas Scaler to: keep constant pixel size , fixes the problem and i now have the correct WORLDtoSCREENpoint that i needed! And now the healthbar floats beautifully above the characters...

BUT WAIT, ANOTHER PROBLEM! Now, if the screen resolution is small... the Ui sprite is obsurdly large..... and if the screen resolution is high definition then Ui sprite is way too small!

QUESTION: So how do i use the "scale with screen size" mode, but yet also still get back a correct WorldToScreenPoint?

ANSWER: you must take into consideration the overal scaling of the canvas when it is stretched to fit (whatever current resolution that you are using)

INSTEAD OF:

 RectTransform myRect = GetComponent<RectTransform>();
 Vector2 myPositionOnScreen = Camera.main.WorldToScreenPoint (myOwner);
 myRect.anchoredPosition = myPositionOnScreen;

YOU CALCULATE THE OVERALL SCALE FACTOR LIKE THIS:

 RectTransform myRect = GetComponent<RectTransform>();
     Vector2 myPositionOnScreen = Camera.main.WorldToScreenPoint (myOwner);
 
 Canvas copyOfMainCanvas = GameObject.Find ("Canvas").GetComponent <Canvas>();
 float scaleFactor = copyOfMainCanvas.scaleFactor
 
 Vector2 finalPosition = new Vector2 (myPositionOnScreen.x / scaleFactor , myPositionOnScreen.y / scaleFactor);
 myRect.anchoredPosition = finalPosition;


If this helped anyone please log in to give me a thumbs up....

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 CalebLee123 · Jul 26, 2020 at 06:05 PM 0
Share

Brilliant man, thanks so much

avatar image steril · Aug 23, 2021 at 04:05 PM 0
Share

Yep, this is the answer we're all looking for.

avatar image
2

Answer by James2Games · Dec 08, 2019 at 03:07 AM

This is a little old topic but for future clarity here are some points I noticed while investigating WorldToScreenPoint in 2019.11.f1.

The point you are given from WorldToScreenPoint is not scaled according to the scale of the canvas. So if your Canvas is scaled in any way including the below - the vector you receive will not be scaled to fit the canvas.

  • Screen Space - Camera

  • World Space

  • Scale Factor


Since I was using Screen Space - Camera - the Canvas was scaled by the camera. I got a relatively precise vector by scaling the WorldToScreenPoint by the scale of the canvas.


I was able to get a precise WorldToScreenPoint by using Screen Space - Overlay.

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

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

Doing a Crosshair for a Space Fight Simulator 0 Answers

Lock On Reticle 1 Answer

How to get size in pixels of an object using SteamVR camera 0 Answers

Worldtoscreenpoint..... 2 Answers

WorldToScreenPoint, isn't getting it right? 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