- Home /
 
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.
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.
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.
Your answer
 
             Follow this Question
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