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 ExTheSea · Jun 03, 2012 at 08:04 PM · guifloatweaponnames

Name Hovering over Weapon Model. Problem!

Hey guys,

I have a problem with a script. I'm trying to implement that the name of a weapon hovers over it. Here is the script:

     function OnGUI ()
 {
 
 for (var go : GameObject in FindObjectsOfType(GameObject)){
     for (var i=0;i<transform.childCount;i++){
          if(go.tag==WeaponsArray[i] && !go.transform.parent){
              if(Vector3.Distance(transform.position, go.transform.position)<20){
              GUI.Box(Rect(Camera.main.WorldToScreenPoint(go.transform.position).x,Screen.height-Camera.main.WorldToScreenPoint(go.transform.position).y,50,20), go.tag);
              }
          
         }
     }
 }
 
 }

Btw: WeaponsArray is an array with all of the weapon names.

The script works in the way that the name does hover over the weapons. But the problem I have is that there are also names on the mirror side. I mean when I turn around there are names hovering on the positions where the weapons were when you would mirror them on the camera.

I searched for this problem and found something but it didn't worked so I hope someone of you can help me.

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

2 Replies

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

Answer by Berenger · Jun 03, 2012 at 08:15 PM

You really shouldn't use any Find function in OnGUI, as it is called several times per frame. About your problem, isn't there a confusion between 2d and 3d ?

Comment
Add comment · Show 5 · 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 ExTheSea · Jun 03, 2012 at 08:23 PM 0
Share

What do you mean with confusion between 2d and 3d?

avatar image Berenger · Jun 03, 2012 at 08:46 PM 0
Share

Don't you expect the names to be on a 3D plane and be reversed when you turn around ? $$anonymous$$aybe I misunderstood the mirror thing.

avatar image ExTheSea · Jun 03, 2012 at 09:11 PM 0
Share

The names are not on a plane they are GUI-boxes. I get the position they should have on the screen through Camera.WorldToScreenPoint. With this technique the gui-boxes are on the 2d-screen but are also over the weapons model. So the names are not in the 3d scene just on the 2d gui. The problem I have is that they also appear when I look in the exact opposite direction of the Weapon$$anonymous$$odel. I hope this clears things up and now someone can help me.

avatar image Berenger · Jun 03, 2012 at 11:09 PM 0
Share

Ooooook, it's displayed even when the weapon is outside the camera's frustrum. It's strange that the 2D coordinate you find are whithin range, but anyway you can use go.renderer.isVisible before drawing the gui.

avatar image ExTheSea · Jun 04, 2012 at 11:36 AM 0
Share

go.renderer.isVisible worked like a charm and I will change the find thing to something more cpu friendly^^. Big Thanks although it seems you didn't quite understand my entire script :).

avatar image
1

Answer by vzdemon · Jun 04, 2012 at 11:56 AM

You have to do some kind of convertion from 3D coordinates to 2D point and that is mighty difficult. what you should do instead is put a 3D text object over each weapon and put this script over it to tell it to only appear if the player is a certain distance from it.

 var player : Transform;
 var minDist : float = 20;
 
 function Update() {
     if(Vector3.Distance(player.position,transform.position)<minDist){
          GetComponent(TextMesh).active = true;
     }else{
          GetComponent(TextMesh).active = false;
     }    
 }
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 ExTheSea · Jun 04, 2012 at 12:08 PM 0
Share

I got it done with the script which is in the question and go.renderer.isVisible. For the convertion I used Camera.WorldToScreenPoint. This function gives back the screen position a 3d object has. ...but thanks for your answer I maybe use it at another point in my game.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Window problem 2 Answers

GUI Button sound problem, don´t work 1 Answer

GUI display weapon name you are looking at 1 Answer

How to cut numbers after decimal point in coordinates? 1 Answer

Pause Menu background problem 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