- Home /
The question is answered, right answer was accepted
Track target
Hi, can someone point me in the right direction please, I'm looking to write a script to track an incoming target across the screen with a GUI texture, like in my picture below, and when the target goes outside the camera view the GUI texture will track the screen edge until the game object it's tracking is destroyed.
So any ideas of the best way to go about this would be appreciated.
Thanks.
Do you have any code snippet?, because if you need help with this you need at least show some part of your code, the tracking part, so any one who see this can have an idea what are you talking about. :-)
No, not started to write it yet, was looking for some pointers so I don't waste a lot of time going down the wrong road.
take a look at
Camera.WorldToScreenPoint
http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html
Also you will probably nee
$$anonymous$$athf.Clamp
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
for keeping the GUI on screen.
Hope thats useful, Scribe
Answer by Griffo · Nov 06, 2012 at 04:17 PM
Ok .. Sorted, needed to track GameObject(Clone) not just the GameObject
Answer by Griffo · Nov 06, 2012 at 03:37 PM
Ok that was easier than I thought it would be, but I have another problem, if I drop a game object onto target in the inspector it tracks it as expected, but if I drop a prefab onto target in the inspector when the prefab is initiated it does not track it, why?
The first script is the tracking script, the second is throw Grenade script, In the throw grenade script it turns on the Track Grenade game object so the tracking script will run, then just before destroying the Grenade game object turns it off again.
Thanks.
#pragma strict
var target : Transform;
var trackIcon : Texture2D;
function Awake(){
}
function Start(){
}
function Update () {
var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
}
function OnGUI(){
var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
var reverse : float = Screen.height;
var clampX = Mathf.Clamp(screenPos.x, 0, Screen.width - (trackIcon.width / 2));
var clampY = Mathf.Clamp(reverse - screenPos.y, 0, Screen.height - (trackIcon.height / 2));
GUI.DrawTexture(Rect(clampX, clampY, trackIcon.width / 2, trackIcon.height / 2), trackIcon);
}
#pragma strict
var grenadeExplosion : Transform;
var Explosion : Transform;
var explosionSound : AudioClip;
var hitSound : AudioClip;
var trackGrenade : GameObject; // Referance to the GameObject with the TrackTarget on
private var creationTime : float;
private var loop : boolean = true;
private var boxCollider : BoxCollider;
private var playerDamage : GameObject;
function Awake(){
creationTime = Time.time;
}
function Start(){
trackGrenade = GameObject.Find("Track Grenade");
playerDamage = GameObject.Find("Main Camera");
trackGrenade.gameObject.SetActive(true);
boxCollider = transform.GetComponent(BoxCollider) as BoxCollider;
}
function Update(){
if(Time.time > (creationTime + 4)){
destroyGrenade();
}
}
// Apply damage to the Player acording to where they are in the blast range
function AreaDamageEnemies(location : Vector3, radius : float , damage : float ){
var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius);
for (var col : Collider in objectsInRange){
var player : FirstPersonControl = col.GetComponent(FirstPersonControl);
if (player != null)
{
// linear falloff of effect
var proximity : float = (location - player.transform.position).magnitude;
var effect : float = 1 - (proximity / radius);
// Apply damage to the Player in the PlayerDamage.js on the Main Camera game object (Player)
playerDamage.GetComponent(PlayerDamage).ApplyDamage(damage * effect);
}
}
}
function OnCollisionEnter(collision : Collision) {
audio.volume = 1.0;
audio.PlayOneShot(hitSound);
}
function destroyGrenade(){
if(loop == true){
loop = false; // Set loop to false so this destroy() function dont keep repeating
AreaDamageEnemies(transform.position, 10, 10); // Set the blast radius of the Grenade and call AreaDamageEnemies function above
boxCollider.size = Vector3(0,0,0); // Resize the Box Collider down to 0 so you dont get an error with the
// RayCast trying to access it after the Grenade has been destroyed
Instantiate(grenadeExplosion, transform.position + Vector3(0, 0, 0), Quaternion.identity);
renderer.enabled = false; // Turn off the mesh render so you dont see the Grenade while the explosion effect and explosion sound sample play out
Instantiate(Explosion, transform.position + Vector3(0, 0.7, 0), transform.rotation);
audio.volume = 2.0;
audio.PlayOneShot(explosionSound);
yield WaitForSeconds (3.0);
trackGrenade.gameObject.SetActive(false);
Destroy (gameObject); // Destroy the Grenade
}
}