Was wondering if I would be able to cast a OverlapSphere and check for tags. I have the script attacked to my turret and it casts a Physics.OverlapSphere when needed and then picks a tarrget from the collected rigidbodies depending on a variable float. But when a different rigidbody enters thats not an enemy I get errors.
function SearchTarget()
{
//inSight = false;
var newTarget : Enemy = null;
var collidersInRange = Physics.OverlapSphere(transform.position,range);
for (var currentCollider : Collider in collidersInRange)
{
var currentEnemy : Enemy = currentCollider.GetComponent.<Enemy>();
if (newTarget == null)
{
newTarget = currentEnemy;
}
else if (newTarget.health > currentEnemy.health)
{
newTarget = currentEnemy;
}
}
if (newTarget != null)
currentTarget = newTarget.transform;
}
As you can see it picks the one with the lowest health.
I got this script from a another Unity Answer post and it works great. Would like to use it, but with a tag. I tried placing the code in various places and nothing worked.