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
2
Question by Orandze · Mar 01, 2011 at 03:33 PM · guigameobjectbuttonpositionmatch

GUI Button and a gameObject's position

Is there an easy way to have a GUI Button show up over a game object? I'm aiming for various items on a desk that have writing on them saying what will happen if the user clicks on them. (E.g. keys for "Shop", paper for "Write".) I was hoping this would be simpler than making the objects clickable and just positioning GUITextures over them.

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

3 Replies

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

Answer by Statement · Mar 01, 2011 at 03:47 PM

You can ask the camera where any point in world space will be in screen space.

Try this to get a feeling (untested code, might contain errors):

public class Example : MonoBehavior { // Bind target to the object it should follow. public Transform target;

 // We use update to obtain the position in this
 // example so we need to keep it until OnGUI.
 Vector3 point;

 void Update()
 {
     // Find screen position for target
     point = Camera.main.WorldToScreenPoint(target.position);
 }

 void OnGUI()
 {
     // In case point.y is inverted, just subtract it from the screen height.
     Rect rect = new Rect(point.x, point.y, 100, 50); 

     if (GUI.Button(rect, "Button")
     { 
         // Button clicked.
     }
 }

}

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 Orandze · Mar 01, 2011 at 04:38 PM 0
Share

Thank you very much. Although I'm mainly a javascripter, you've definitely put me on the right track. I'll need to do a little more tweaking to get it working just right, but at least I now know what I have to do. Thanks again!

avatar image octaviomejiadiaz · Jul 09, 2012 at 02:43 AM 0
Share

hi, i didnt understand where do i have to subtract the the point.y im nbot sure if im using scree height i have this:

  void Update()
 {
     // Find screen position for target
     point = Camera.main.WorldToScreenPoint(target.position);
 }
 
 
  void OnGUI()
  {
  float yPos = 5.0f;
  float xPos = 5.0f;
  float width = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
  float height = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
  float heightPlus = height + 0.0f;
  
  
  
  // In case point.y is inverted, just subtract it from the screen height.
     Rect rect = new Rect(point.x, point.y, 100, 100); 






** I've tried change the screen height that appears in the code but, that is for some other static GUI buttons.

And one more question, how do put a position for example if want that the GUI button follow the object but in a specific position (a square object and the GUI button follow it but always in the corner)

appreciate the help!

avatar image
1

Answer by robert 4 · Mar 01, 2011 at 03:36 PM

http://answers.unity3d.com/questions/5045/using-a-gui-button-to-stop-a-gameobject-movement

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 octaviomejiadiaz · Jul 09, 2012 at 06:21 AM

hi, i didnt understand where do i have to subtract the the point.y im nbot sure if im using scree height i have this:

  void Update()
 {
     // Find screen position for target
     point = Camera.main.WorldToScreenPoint(target.position);
 }
 
 
  void OnGUI()
  {
  float yPos = 5.0f;
  float xPos = 5.0f;
  float width = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
  float height = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
  float heightPlus = height + 0.0f;
  
  
  
  // In case point.y is inverted, just subtract it from the screen height.
     Rect rect = new Rect(point.x, point.y, 100, 100); 






** I've tried change the screen height that appears in the code but, that is for some other static GUI buttons.

And one more question, how do put a position for example if want that the GUI button follow the object but in a specific position (a square object and the GUI button follow it but always in the corner)

appreciate the help!

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

1 Person is following this question.

avatar image

Related Questions

Is it possible to position a GameObject as relative to a GUI? 2 Answers

Button then instanitates gameobject 1 Answer

how to toggle on/off a gameobject with a GUI button ? 3 Answers

Game Object Toggle GUI Button Errors 1 Answer

Bullet Prefab not instantiating 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