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 spaceshooter · Dec 14, 2010 at 08:39 PM · worldspacescreenspaceworldtoscreenpoint

Worldspace Conversion

I'd like to place a Label above a GameObject. To do this, it seems I need to convert the GameObjects worldspace coordinates to screenspace. Anyone have an example of doing this? Thanks!

I'm thinking it goes something like this...

    Vector3 sp = camera.WorldToScreenPoint(transform.position);
    ???
    Rect r = new Rect(???);
    GUI.Label(r, "Hello World!");
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
0
Best Answer

Answer by The_r0nin · Dec 14, 2010 at 09:02 PM

In javascript:

var mainCam: Camera = Camera.main; //I always cache component variables used frequently.

OnGUI(){

screenLoc: Vector3 = mainCam.WorldToScreenPoint(transform.position); GUI.Label (Rect(screenLoc.x-25,screenLoc.y-20,50,20),"My Tag");

That should give you a label 20 pixels above your object center and centered over it (50 pixels wide). If you want the label to be above the top of the object, use renderer.bounds (and then min/max or extends). A script that labels objects (for you to look at) in the wiki is HERE.

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 spaceshooter · Dec 14, 2010 at 09:18 PM 0
Share

It only seems to work when the GameObject is close to the center of the screen, As it gets near the top and bottom, the space between the text and object gets larger. This is a 2D,setup with the camera along the z-axis. Thanks!

avatar image spaceshooter · Dec 14, 2010 at 11:51 PM 0
Share

That script you pointed me too is very helpful, thanks!

avatar image weezma2004 · Jul 30, 2012 at 06:10 PM 0
Share

Just a heads up to others, WorldToScreenPoint() gives a y position in relation to the bottom of the screen being 0, whereas GUI has 0 at the top of the screen. So if your camera is not stationary (e.g. player jumps), the label over the game object will rise. To fix this use this for the y parameter of the Rect:

Screen.height - screenLoc.y - 20

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

1 Person is following this question.

avatar image

Related Questions

How to move camera close to a surface such that the pixel size on the surface match the pixel size of the screen? 0 Answers

how to get the most right point or left point atc of object as visual on the screen in 3d project? 0 Answers

Zoom in on tapped point in 3D space 1 Answer

UI object marker showing up when object is outside view 0 Answers

How to get 3D worldspace position from 2D touch/mouse position 3 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