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 kennypu · Dec 31, 2010 at 10:05 PM · guipositionlabeldrawing

Trying to draw GUI.Label() at object position

Hey, I have a quick question

I'm trying to draw a GUI.Label() at an gameobjects position. I got the function I need, and I tested it out, and it works, but its not quite right. I'm using the standard (I believe) FPC for the character. The Script for the label is as follows:

    var bins:GameObject[] = GameObject.FindGameObjectsWithTag("bin");
for (var bin:GameObject in bins)
{
        /*find bin coordinates and make label*/
        var p = Camera.main.WorldToScreenPoint(bin.transform.position);
        GUI.Label(Rect(p.x,p.y,200,30),bin.name);
}

what happens is, as the camera moves, the x position of the label is fine, and where its suppose to be. however, the Y coordinate seems to go way off, just like if a pivot point for an object is off when you're rotating it. Any idea what I'm doing wrong? Thanks.

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

5 Replies

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

Answer by kennypu · Dec 31, 2010 at 11:15 PM

Ok I figured out what was going on. Apparently, the WorldToScreenPoint and WorldToViewportPoint functions return an integer(or float respectively) starting from 0,0 at the BOTTOM LEFT of the screen. Because of this, the y-value that I was getting was basicly flipped. So to solve the problem, I just had to subtract the value we got by the height to get the real coordinates. hopefully this helps any one else that was wondering :)

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
avatar image
5

Answer by alejandro__ · Feb 09, 2017 at 07:55 AM

So in case that anyone falls in here, here's a solution with code:

   private string text;
 
   void OnGUI() {
     var position = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     var textSize = GUI.skin.label.CalcSize(new GUIContent(text));
     GUI.Label(new Rect(position.x, Screen.height - position.y, textSize.x, textSize.y), text);
   }
Comment
Add comment · Show 1 · 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 ericewers · Nov 14, 2019 at 01:03 AM 0
Share

Thank you so much! This was exactly what I was looking for!

avatar image
0

Answer by DaveA · Dec 31, 2010 at 10:09 PM

Just a guess, but try using p.z where you have p.y right now.

Comment
Add comment · Show 1 · 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 kennypu · Dec 31, 2010 at 10:58 PM 0
Share

that almost worked. When giving the z value, it doesn't give the weird problem where the positions get thrown off a lot, but now the y value just stays in the same spot on the screen (so if I look up, its in the same spot rather than moving down to match the position of the object).

avatar image
0

Answer by PrimeDerektive · Dec 31, 2010 at 10:21 PM

Did you try setting a custom gui style to the label and playing with the alignment of the text? That sometimes throws me off.

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
avatar image
0

Answer by Bryan-Legend · Nov 10, 2013 at 10:50 PM

Here's what I'm using. It's not perfect but close enough for debugging. Any idea on how it could be fixed?

     void OnGUI()
     {
         var point = Camera.main.WorldToScreenPoint(transform.position);
         GUI.Label(new Rect(point.x, Screen.currentResolution.height - point.y - 200, 200, 200), name);
     }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I position a GUI Label over an object? 1 Answer

GUI Labels Overlap 2 Answers

Auto positioning GUI Labels? 0 Answers

GUI Label Size and Position Changes With Resolution 2 Answers

GUI.label overlapping text 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