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 Jeff-Kesselman · Mar 06, 2014 at 05:39 PM · worldtoscreen

How to find screen pixel world object coord drawn in?

I'm attempting to map an object serving as a 3D cursor back to a screen position where coordinate 0,0 on the 3D cursor object is the "hotpoint" I want mapped.

I would have thought that this would do it:

 public Vector2  GetScreenPosition(){
     Vector3 screenPos = 
            Camera.main.WorldToScreenPoint(transform.position);
     return screenPos;
 }

But I am getting a drift. It is correct at screen center but not as I move away from center, as evidenced by this code:

 void OnGUI() {
     Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
     GUI.Box(new Rect(screenPos.x-5,(Screen.height-screenPos.y)-5,
             11,11),"X");
 }

Is there some other transformation involved in that that I am missing?

Addl info: There seems to be little to no drift when a flat cursor is drawn at the clipping plane, but the further back in the scene the cursor is drawn, the more pronounced the drift is.

This says to me that WorldToScreen is not correctly figuring for perspective correction?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by wibble82 · Mar 06, 2014 at 06:04 PM

Hi there

That code works perfectly for me (I tried your OnGUI function in my code).

Are you sure you aren't just getting confused about where the transform.position is?

If you picture your object as having a centre point (transform.position), and the front surface (that you can actually see), which is probably a bit closer to the camera (unless your object is totally flat). When the object is dead centre in screen space, its centre point will also appear right in the middle of its front surface.

However, with a perspective camera, as the object moves closer to the edge of the screen its centre point will stop appearing to be at the centre of its front surface. Imagine taking a 3D line from the centre of your object to the exact position of the camera. Your X is being drawn where that lines starts, but where the line intersects the surface of your object will only be in the centre when the the object is right in front of the camera.

Try your code with a very flat object and see what happens. If the drift goes away, there's your issue. It can be solved in various ways depending on the effect you want.

Comment
Add comment · Show 3 · 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 Jeff-Kesselman · Mar 06, 2014 at 06:09 PM 0
Share

Good point and you are almost certainly right.

Which means I need to rethink the 3D cursor... it may need to be a planar billboard rather then a real 3D object...

avatar image Jeff-Kesselman · Mar 06, 2014 at 06:27 PM 0
Share

Spoke too soon, with a flat textured quad, it shows the exact same drift :/

avatar image Jeff-Kesselman · Mar 06, 2014 at 06:33 PM 0
Share

So, if I put the quad right at the clipping plane, it seems to work or at least have so little drift as to be unnoticeable.

But further back in the scene it drifts.

This says to me that WorldToScreenPoint is not calculating in the perspective correction??

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

21 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

Related Questions

WorldToScreenPoint bad return at large values 0 Answers

draw canvas/2D space Box Colliders on top of 3D objects that are located in world space? 0 Answers

convert world to screen (crosshair follow) 1 Answer

HealthBar for 5 Objects 3 Answers

Converting Mouse Position to World. Stationary Camera. 4 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