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 MigLeader · Jun 16, 2012 at 01:16 AM · javascriptguiarraytargetinghighlight

drawing GUI or Instantiate an object to highlight multiple object

hi there my friends , i encountered a problem with GUI and texture2d and objects , here wt is it:

1-i have made a script that looks for objects tagged as "Targets" and add them to an array.
2-i want that script to look for every object it finds in the array and get their positions and the size of their meshes how big they appear to my camera(Far = smaller , near = bigger).
3-i want it then to either:-

a)using a GUI to draw a 2dTexture around each target automatically and update as they moves(the Targets).
b)to Instantiate a GUITexture object (or any other object) on top of each target and update as they moves(the Targets).

here are my codes:

the main Script

 var targets : GameObject[];
 var targetFrame : GUITexture;
 var targetFramePos : Vector3[];
 var t : int;  
     
 function Start () {  
         targets = GameObject.FindGameObjectsWithTag("Targets");  
 }
 
 function OnGUI(){  
     for( t = 0 ; t < targets.length ; t++){  
         targetFramePos[t] = camera.main.WorldToScreenPoint(targets[t].transform.position);  
         GUI.Box(Rect(targetFramePos[t].x,targetFramePos[t].y,60,60),"" + targets[t]);  
     }  
 }  


the above code works the most best but the GUIs are not aligned on the center , and if the camera turns around they get messed up pretty bad

2nd code

 var targets : GameObject[];  
 var highLightFrame : GUITexture; 
 var t : int;  

 function Start () {  
     targets = GameObject.FindGameObjectsWithTag("Targets");  
 }  
 
 function Update () {  
     for( t = 0 ; t < targets.length ; t++){  
         Instantiate(highLightFrame, camera.main.WorldToViewportPoint(targets[t].transform.position),transform.rotation);  
         highLightFrame.transform.position = camera.main.WorldToViewportPoint(targets[t].transform.position);  
     }  
 }

the above code also works but i must add the following script to the (highLightFrame) and still if i looked around (180 deg) the (highLightFrame) appears in front of me even if the targets are behind:

 function Start () {  
     yield WaitForSeconds(0.01);  
     Destroy(gameObject);  
 }  

in simple i want to do the logic of a radar but not like the one i find in the Wiki , bcz it renders the dots as 2D plane , i want it to be like a hud that detects targets and organize them , i will experiment more on the rest logic, i love to experiment a lot but this one really troubled me , right now i am stuck at drawing around each target something to show in the hud.

Note:i tried using mesh.bounds , renderer.bounds but i failed at using them even after reading the Docs.

it will be really a great help even if its a small hint.

so thanks for anyone reading my problem and helping me out.

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

Answer by MigLeader · Jun 17, 2012 at 10:56 PM

bcz still no one has helped me with this problem , i worked more on it and i came with a fix that is ok for now , here is the code in case someone wanted it:

 var targets : GameObject[];
 var aTexture : Texture2D;
 var mainShip : GameObject;
 var position : Vector3;
 
 function Start () {
     targets = GameObject.FindGameObjectsWithTag("Targets");
 }

 function OnGUI(){
         for(var t : int = 0 ; t < targets.length ; t++){
             var size = 1/Vector3.Distance(mainShip.transform.position, targets[t].transform.position) * 1000;
             var size2 = 1/Vector3.Distance(targets[t].transform.position,mainShip.transform.position);
             position = camera.main.WorldToScreenPoint(targets[t].transform.position);
             
             if(position.x > Screen.width){
                 position.x = Screen.width;
             }else if(position.x < 0){
                 position.x = 0;
             }
             
             if(position.y > Screen.height){
                 position.y = Screen.height;
             }else if(position.y < 0){
                 position.y = 0;
             }
             
             if(position.z < 0){
                 position.y = 0;
             }
             
             position.y = Screen.height - position.y;
             GUI.DrawTexture(Rect((position.x - (size/2)), (position.y - (size/2)), size, size), aTexture);
             GUI.Label(Rect((position.x - (size/2)), (position.y + (size/2)), size * 20, size * 2),"Target = " + targets[t].name);
             GUI.Label(Rect((position.x - (size/2)), (position.y + (size/2)+15), size * 20, size * 2),"Dist = " + 1/size2);
         }
 }


and still , if anyone finds a way to optimize the code by making it (easier , faster , manageable) feel free to edit it.

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 MigLeader · Jun 17, 2012 at 12:37 AM

ok , as no one has answered my question i experimented little more and found a solution here to my problem but there is still one problem left , here is the code:

 var targets : GameObject[];
 var aTexture : Texture2D;
 var mainShip : GameObject;
 
 function Start () {
     targets = GameObject.FindGameObjectsWithTag("Targets");
 }
 
 function OnGUI(){
     for(var t : int = 0 ; t < targets.length ; t++){
         var size = 1/Vector3.Distance(mainShip.transform.position, targets[t].transform.position) * 1000;
         var position = camera.main.WorldToScreenPoint(targets[t].transform.position);
         position.y = Screen.height - position.y;
         GUI.DrawTexture(Rect((position.x - (size/2)), (position.y - (size/2)), size, size), aTexture);
     }
 }

it renders a GUI texture nicely on each object , but there is only 1 problem left , when i turn around 180 Deg , i can see the GUI texture also in front of my even if the targets are behind me.

i hope at least i find fix for this problem now ,please and thx.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What type of Array should I use? 1 Answer

Arrange ints from biggest to smallest and display them in a table 1 Answer

How can i show the texture in the array? 2 Answers

More like a bulletin than chat box 0 Answers

Multiple enemy indicators 2 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