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.