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 david1902 · Jun 22, 2012 at 05:52 PM · javascriptguiongui

Weird GUI error (script included)

On screen: plane, cube(rigidbody), camera, directional light, first person standard asset.

Now add this script to an empty game container. Add cube as transform to target.

Run.

GUI appears over cube, great. Now turn around, the GUI is also behind you.

Why is this? It should only work when cube is visible right? I don't want the GUI behind me! What am I doing wrong?

var target : Transform;

var barDisplay : float = 0; var pos : Vector2 = new Vector2(Screen.width/2,Screen.height/2); var size : Vector2 = new Vector2(100,20); var progressBarEmpty : Texture2D; var progressBarFull : Texture2D;

function OnGUI() { GUI.BeginGroup (new Rect (pos.x*Screen.width, (1-pos.y)*Screen.height, size.x, size.y)); GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty); GUI.EndGroup (); }

function Update () { pos=Camera.main.WorldToViewportPoint(target.position); print(Camera.main.WorldToViewportPoint(target.position)); }

Any idea?

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 whydoidoit · Jun 22, 2012 at 05:57 PM

Don't display the GUI if the item is behind you using the Dot product of the positions like this:

   function OnGUI() {
         if(Vector3.Dot(target.position - transform.position, transform.forward) < 0) 
                 return;
   }
Comment
Add comment · Show 6 · 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 david1902 · Jun 22, 2012 at 06:18 PM 0
Share

$$anonymous$$ike, any idea why it is doing this? From the code it should only work when the object is in the camera view, right?

avatar image whydoidoit · Jun 22, 2012 at 06:21 PM 0
Share

Nah, it does that on the viewport point when the object is behind you...

avatar image whydoidoit · Jun 22, 2012 at 06:21 PM 0
Share

It ignores the Z - kind of to do what it does :)

avatar image david1902 · Jun 22, 2012 at 06:28 PM 0
Share

The above fix works, thanks, but it seems a little band-aidy... are there any other ways to fix it? Would rearranging the scene work somehow? or any other way?

avatar image whydoidoit · Jun 22, 2012 at 06:30 PM 0
Share

Nope - getting a viewport point from a world point will return a coordinate that projects the Z onto the screen from either direction - you kind of need it to work that way in some circumstances.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

using GUI scrollView for multiple onGUI functions 0 Answers

GUI opacity. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

GUI Not updating 1 Answer

turn off items from GUI 0 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