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 richardzzzarnold · Dec 09, 2010 at 03:54 PM · guibuttonfollowobjectlabel

ObjectLabel working inversely in y direction

I am using a variation of the ObjectLabel script to keep a GUI Button tracking a character around the screen. But while it almost works, the screen-y component is behaving strangely. As the character moves upwards on the screen, the button goes correspondingly downwards from the center of the screen, and vice versa. I am sure that it is a simple fix but cannot work it out. The screen x component works great.

var anims : String[] = ["jump","hop","dance"]; //animations var buttonTexture : Texture; //The texture to use on the button var offset = Vector3.up; //World space offset. 1 unit above object by default var clampToScreen = false; //Will the label be visible if object is off screen var clampBorderSize = .05; //screen space to leave when clamped var useMainCamera = true; //Use the camera tagged MainCamera var cameraToUse : Camera; //Camera to use if useMainCamera is false private var cam : Camera; //The camera we're using private var screenPos : Vector3; //The screen position of the button

 function Start () {
     if(useMainCamera) cam = Camera.main;
     else cam = cameraToUse;
 }

 function Update () {
     if(clampToScreen) {
         var relativePosition = cam.transform.InverseTransformPoint(transform.position);
         relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
         screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset));
         screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)),Mathf.Clamp(screenPos.y, clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)),screenPos.z);
     }
     else screenPos= cam.WorldToScreenPoint(transform.position + offset);
 }

 function OnGUI() {
     if(GUI.Button(Rect(screenPos.x,screenPos.y,buttonTexture.width,buttonTexture.height),buttonTexture))
         animation.CrossFade(anims[Random.Range(0,anims.length)]);
 }

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
Best Answer

Answer by Statement · Dec 09, 2010 at 04:07 PM

Rect(screenPos.x, screenPos.y, 
     buttonTexture.width, buttonTexture.height)

Try changing that to:

Rect(screenPos.x, Screen.height - screenPos.y, 
     buttonTexture.width, buttonTexture.height)

Because I guess your coordinates are just flipped.

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 richardzzzarnold · Dec 09, 2010 at 04:21 PM 0
Share

Thanks ! Works perfect now !

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

No one has followed this question yet.

Related Questions

Adjusting ObjectLabel to activate animations 1 Answer

GUI Button follow GameObject, like floating button how to on C++? 2 Answers

GUI Button Enable/Disable 1 Answer

How to put a GUILayout button BESIDE another one instead of below? 1 Answer

Rect to RectTransform on overlay Canvas? 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