- Home /
making turret to kill each other but with same tag
when i add tag to parent game object the turret takes target parent game object, i want turret to take target all except the parent on which the child script is atached code is
public Transform partToRotate;
// Use this for initialization
void Start () {
InvokeRepeating("UpdateTarget", 0f, 0.5f);
}
void UpdateTarget ()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag (enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range) {
target = nearestEnemy.transform;
}
else
{
target = null;
}
}
// Update is called once per frame
void Update () {
if (target == null)
return;
Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(-dir);
Vector3 rotation = lookRotation.eulerAngles;
partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, range);
}
Comment
Your answer
Follow this Question
Related Questions
Optimizing enemy AI and a question about speed differences between C# and Javascript 2 Answers
enemy AI script 3 Answers
Turret AI script off rotation 1 Answer
Turret AI Script 1 Answer
Turret Movement 1 Answer