Hi, I’m still pretty new to unity and coding. I’m making a tower defense game, with enemies that follow a set of waypoints.
I’m trying to add a second set of waypoints and the part I’m having trouble with is getting the enemies to spawn and follow a random path.
This is my code on the waypoint script.
public class Waypoints : MonoBehaviour
{
public static Transform[] points;
private void Awake()
{
points = new Transform[ transform.childCount];
for (int i = 0; i < points.Length; i++)
{
points *= transform.GetChild(i);*
}
}
}
This is the script that moves the enemy
public class EnemyMovement : MonoBehaviour
{
private Transform target;
private int wavepointIndex = 0;
private Enemy enemy;
public Transform healthBarCanvas;
void Start()
{
enemy = GetComponent();
target = Waypoints.points[0];
}
void Update()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);
transform.LookAt(target);
healthBarCanvas.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
if (Vector3.Distance(transform.position, target.position) <= 0.5f)
{
GetNextWaypoint();
}
enemy.speed = enemy.startSpeed;
}
void GetNextWaypoint()
{
if (wavepointIndex >= Waypoints.points.Length - 1)
{
EndPath();
return;
}
wavepointIndex++;
target = Waypoints.points[wavepointIndex];
}
I’ve been trying to use an array of arrays [][] but I can’t work it out. Any help would be greatly appreciated.