Currently I am using the ObjectLabel script to put a targetting reticle over targets in my space simulation game. But it works for just 1 target. I want the targetting reticle appear over all targets around the player.
So I created sphere with a collision trigger and added this script:
`
private var targetList : Array = Array();
private var targetPosition : Vector3;
var targetReticle : GameObject;
function Start ()
{
}
function Update ()
{
}
function OnTriggerEnter (other : Collider)
{
targetPosition = other.gameObject.transform.position;
targetReticle.SendMessage("AddItem",targetPosition);
//Sends the target's position to the TargetReticle GUITexture object
}
`
Then I modified the ObjectLabel script as follows:
`
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
//CUSTOM VARIABLES
private var targetList : Array = Array(); //List of Targets are stored here
var arrayLenght : int; //Lenght of the target array
private var i : int = 0;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
}
function Update ()
{
var arrayLenght = targetList.length;
for(i = 0; i <= arrayLenght; i++)
{
Debug.Log("Value of i:"+i);
Debug.Log("Position of the target:"+targetList*);*
-
Debug.Log("Array lenght:"+arrayLenght);* -
if (clampToScreen) {*
_ var relativePosition = camTransform.InverseTransformPoint(targetList*);*_
* relativePosition.z = Mathf.Max(relativePosition.z, 1.0);*
* thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));*
* thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),*
* Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),*
* thisTransform.position.z);*
* }*
* else {*
* thisTransform.position = cam.WorldToViewportPoint(target.position + offset);*
* }*
}
}
//CUSTOM FUNCTIONS HERE
function AddItem(item : Vector3)
{
targetList.Add(item);
}
@script RequireComponent(GUIText)
`
The code actually does get all the targets in the dedection sphere BUT it only puts the reticle on the last object in the array. How do I change the code to keep the reticle on till the objects get destroyed?
Thanks.