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 Chimera3D · Nov 03, 2012 at 10:24 PM · guiobjectscreenfollowview

GUI Follow Object On-Screen

I wrote some code that's supposed to position a gui element over an object, however whenever the object isn't in the camera view the element is supposed to stop and remain in the camera view but still follow the object. The code I wrote works on the x axis, that is it works as I explained it should, however, on the y axis for some reason the movement is inverted - if I move the object up the element goes down - not only that but it also sometimes just isn't following the object correctly and if the object goes out of the screen it will restart at the bottom of the screen and follow it. The element will be offset while I didn't manipulate any of the values for that to happen. Can anyone tell me how to fix this?

Here is my code:

 var texture : Texture2D;
 var angle : float;
 var objective : GameObject;
 var desiredPos : Vector2;
 var size : Vector2;
 var minimumX : float;
 var maximumY : float;
 var maximumX : float;
 var minimumY : float;
 var rect : Rect;
 var pivot : Vector2;
 var screenPos : Vector2;
 var distance : float;
 var dDistance : int;
 var unit : String;
 
 function Update () {
     
     screenPos = Camera.main.WorldToScreenPoint (objective.transform.position);
     desiredPos.x = ClampPosition (screenPos.x, minimumX, maximumX);
     desiredPos.y = screenPos.y;
     
     if(desiredPos.y > maximumY){
     
         desiredPos.y -= maximumY;
         
     }else
     
     if(desiredPos.y < minimumY){
          
         
         desiredPos.y += minimumY;
         
     }
        
        rect = new Rect(desiredPos.x, desiredPos.y, size.x, size.y);
     pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
 
 }
 
 function ClampPosition (pos : float, min : float, max : float){
     
     if (pos < min)
     pos += min;
     
     return Mathf.Clamp (pos, min, max);
     
 }
 
 function OnGUI()
 {
     
     var matrixBackup : Matrix4x4 = GUI.matrix;
     GUIUtility.RotateAroundPivot(angle, pivot);
     GUI.DrawTexture(rect, texture);
     GUI.matrix = matrixBackup;
     
 }
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
2
Best Answer

Answer by XienDev · Nov 04, 2012 at 07:22 AM

See: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html, it says, that : The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight).

and GUI (0,0) is top left. thats why it's inverted. to flip u can use

rect = new Rect(desiredPos.x, Screen.height - desiredPos.y, size.x, size.y);

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 Chimera3D · Nov 04, 2012 at 07:57 AM 0
Share

Thanks man... I saw that but for some reason it didn't click

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

10 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

Related Questions

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

Make GUI follow the object 1 Answer

Make GUI elements disappear 3 Answers

View through objects 2 Answers

How to follow a moving object by camera on a line ? 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