Auto-targeting help

Hi,

The following script is what I use in an attempt to have an ‘auto-target’ system. What happens is:

  1. Finds all game objects tagged ‘Enemy’.

  2. Calculates the distance between transform(the player) and the game object.

  3. If the distance is less than or equal to 5, change some values.

What I expect to happen:

  1. When player is in range of game object tagged ‘Enemy’, the player’s variable ‘target’ changes to the object within range.

  2. When player leaves that range, the target goes to null.

  3. When the player reenters ANY range of a object with tag ‘Enemy’, the player’s variable ‘target’ becomes that object.

What actually happens:

  1. When a player enters a range of an object with tag ‘Enemy’, the player’s variable ‘target’ becomes that object. GOOD

  2. When the player exits the range, the variable goes to null. GOOD

  3. Reentering the same object’s range, rechanges the variable to that object. GOOD

  4. BUT, it only works with one gameobject with tag ‘Enemy’. Entering a different object that is identical to the other, will NOT change the player’s variable. The only way it will, is if the one that works is destroyed.

Here’s the script:

#pragma strict

function Start()
{
}

function Update () {

CheckForMob();

}


function CheckForMob()
{

var targets: GameObject[];
targets = gameObject.FindGameObjectsWithTag("Enemy");

for(var primarytarget in targets)
{


var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position); 


if(primtargetdistance <= 5)
{

transform.GetComponent(meleecombat).target = primarytarget.transform;

}
else
{

transform.GetComponent(meleecombat).target = null;

}

}
}

Here’s the ‘meleecombat’ script that this one refers to:

#pragma strict

var target: Transform;


function Update () {



if(Input.GetKeyDown(KeyCode.Mouse0))
{
if(target == null)
{
print("You have no target!");
}
else
{
Attack();
}
}



}


function Attack()
{

var distance: float = Vector3.Distance(transform.position, target.transform.position);

var dir : Vector3 = (target.transform.position - transform.position).normalized;
var direction: float = Vector3.Dot(dir,transform.forward);
var hitnum = Random.value;




if(distance < 2.5 && direction > 0.70)
{


if(hitnum >= 0.19)
{
print("You attacked the target!");
target.GetComponent(enemy).AdjustHealth(-5);
var currhealth : float = target.GetComponent(enemy).currhealth;
DeathCheck(currhealth);
}
else
{

print("You missed the target!");

}

}



}
function DeathCheck(number:float)
{

if(number == 0)
{
Destroy(target.gameObject);
target = null;

}

}

your code only works for one enemy, because you continue checking for enemies after you found one, if the last enemy you are checking is to far away you set the target to null regardless of all the previous enemies.

i rewrote your CheckForMob function so it loops through all the enemies and stops when it finds an enemy close enough.

function CheckForMob()
{
   var targets: GameObject[];
   var target: GameObject;

   targets = gameObject.FindGameObjectsWithTag("Enemy");
   for(var primarytarget in targets)
   {
      var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position); 
 
      if(primtargetdistance <= 5)
      {
         target = primarytarget.transform;
         break;
      }
   }

   transform.GetComponent(meleecombat).target = target;
}