Hey Guys,
I have a horde of Zombies (of course) that are attacking a group of towers, each tagged “Tower”. The script works okay, but I want the zombies to attack the closest building first, then the next closest, and so on. I’m sure I need an array to do this, but have little experience. Here’s my code thus far:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance = .5f;
private Transform myTransform;
NavMeshAgent agent;
void Awake () {
myTransform = transform;
}
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
void Update () {
GameObject go = GameObject.FindGameObjectWithTag("Tower");
target = go.transform;
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//move towards Target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
What would be the best way to add the array and make this work?
Thanks,
Stef