- Home /
on keypress find closest target, then change its tag?
Ive been working on a space rts type game, basicly im trying too get the closest transform, wich ive done from looking at awnsers here, and the code works for that well at this point. the Array returns the closest targets Transform just fine when the FindManual(enemiesTargs) is called on KeyCode.Tab, heres where Im not sure and need help. I dont get any errors wit this at all, however when I go too hit tab, All the targets on the screen get changed? Ive tryed about 10 diffrent methods too do this all resulting in ALL the targets getting there name changed, Im guessing its something too do with the way Im instancing my Enemys? they all are enemeys(Clone) and all start with Tag "none". Or target_data keeps passing by All the arrayed objects when I call it on the keypress?.
I need a way too do this, and only change the Tag of the closest enemy returned when tab is pressed.
var target_data : Transform; var myPosition : Vector3; var enemiesTargs : Transform[];
function changeClose(){ //Each enemey spawned has this sensor, if its changeTag is true it changes its own tag too "player1_target" target_data.GetComponent("mouse_sensor").changeTag = true; //change closest targets tag? }
function FixedUpdate() { if(Input.GetKey(KeyCode.Tab)) //Tab pressed, Find closest, load target_data? GetManual(); target_data = FindManual(enemiesTargs); changeClose(); //where I change the tag of the closest? }
function Update() { myPosition = this.transform.position; //This is the players position too check agianst. }
function GetManual () { var enemyObjects = GameObject.FindGameObjectsWithTag("none"); enemiesTargs = new Transform[enemyObjects.Length]; for (i = 0; i < enemyObjects.Length; i++) { enemiesTargs[i] = enemyObjects[i].transform;
}
}
function FindManual (targets : Transform[]) : Transform { var closestDistance = (enemiesTargs[0].position - myPosition).sqrMagnitude; var targetNumber = 0; for (i = 1; i < targets.Length; i++) { var thisDistance = (enemiesTargs[i].position - myPosition).sqrMagnitude; if (thisDistance < closestDistance) { closestDistance = thisDistance; targetNumber = i; } } return enemiesTargs[targetNumber]; }
Answer by aldonaletto · Sep 12, 2011 at 03:41 AM
If you just want to change the tag of the nearest enemy when Tab is pressed, I think this is the easier way to do that:
function Update(){
if (Input.GetKeyDown(KeyCode.Tab)){
var enemies = GameObject.FindGameObjectsWithTag("none");
var here: Vector3 = transform.position;
var minDist: float = Mathf.Infinity; // init with max possible value
var nearest: GameObject;
for (var enemy: GameObject in enemies){
var dist = Vector3.Distance(enemy.transform.position - here);
if (dist < minDist){
nearest = enemy;
minDist = dist;
}
}
if (nearest){
nearest.GetComponent("mouse_sensor").changeTag = true;
}
}
}
Your answer
Follow this Question
Related Questions
drawing GUI or Instantiate an object to highlight multiple object 2 Answers
Find Target From List 1 Answer
C# Targetting Help 1 Answer
Array.map Support 1 Answer
Why Does Initialising Built-In Array of Classes Require Constructor? 1 Answer