x


FindClosestEnemy() change target?

Hi, i'm using the function I found in Unity Script Reference, about FindGameObjectWithTag.

Then I must be able to change between the targets tagged as "Enemy". How would i do in this code?:

function FindClosestEnemy () : GameObject {

var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy"); 
var closest : GameObject; 
var distance = Mathf.Infinity; 
var position = transform.position; 

for (var go : GameObject in gos)  { 
    var diff = (go.transform.position - position);
    var curDistance = diff.sqrMagnitude; 
    if (curDistance < distance) { 
        closest = go; 
        distance = curDistance; 
    } 
    if (Input.GetButton("Jump")) {
        //What would i do here??? how?
    }
} 
return closest;    

}

The problem is that it is an array, and doesn't looks like an array, so then i don't know how to increment "gos[THIS]"....

Thank you so much! :D

more ▼

asked Aug 18 '10 at 06:14 PM

Henrique gravatar image

Henrique
93 14 14 17

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Your question made sense right up until the end. It is an array and looks like one just fine. You can't increment a gameobject and you already are iterating through the array if that's what you were trying to convey.

What you've got there is a foreach loop. It iterates through every element in the array. it is essentially equivalent to

for (var i = 0; i < gos.length ; i++)
{
    var go = gos[i];
    if(go)
    {
        //...the contents of your foreach loop
    }
}

Your calculation of the closest enemy is fine except for the last if statement. Why are you trying to do something when you jump for every loop iteration? The if statement should not be inside your function at all let alone this loop.

I think you want something like:

function FindClosestEnemy () : GameObject {
    var gos : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 

    for (var go : GameObject in gos) { 
        if((go.transform.position - position).sqrMagnitude < distance) { 
            closest = go; 
            distance = curDistance; 
        } 
    }
    return closest;  
}

function Update()
{
    var target : GameObject = FindClosestEnemy();
    if (Input.GetButton("Jump")) {
        //Do whatever you want here.
    }

}
more ▼

answered Aug 18 '10 at 06:37 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

"Jump" is only for test. It is a test project in Unity. My question is how would i use the button "Jump" to change between the enemies...from nearest to far enemies.... I want only change the target... How?

Thx :D

Aug 18 '10 at 06:52 PM Henrique

I never asked about "Jump" or whether it is a test - that has no bearing on anything. I don't see where you are having difficulty with the answer. There is a function to find the closest enemy right here that will work for you and if you want to do that when they push jump, then do so - it's not hard: function Update(){if (Input.GetButton("Jump")) target = FindClosestEnemy();} If you don't want to find the closest enemy, then don't ask about FindClosestEnemy(). If you want to simply find the next-closest-target, then I would ask that you revise your question to ask that.

Aug 24 '10 at 06:45 AM skovacs1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2075
x1356
x652
x242
x205

asked: Aug 18 '10 at 06:14 PM

Seen: 2745 times

Last Updated: Aug 18 '10 at 06:14 PM