So… I decided that i would start trying to create a Tower defense game by following a Tutorial done by Brackeys.
I am aware that these are old tuorials. I am on the Second tutorial where it teaches you how to make an enemy follow waypoint by using scripts. Link
Now i followed his instructions to a T and double checked it over again. Every time i go to test to see if my Enemy is following the waypoints, the enemy doesn’t move! It just sits there. There’s no errors in my scripts. I don’t know, maybe i need another set of eyes on these scripts?
My Enemy Script:
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10f;
private Transform target;
private int wavepointIndex = 0;
void Start()
{
target = waypoints.points[0];
}
void update ()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) <= 0.4f)
{
GetNextWaypoint();
}
}
void GetNextWaypoint()
{
if (wavepointIndex >= waypoints.points.Length - 1)
{
Destroy(gameObject);
return;
}
wavepointIndex++;
target = waypoints.points[wavepointIndex];
}
}
My Waypoint Script:
using UnityEngine;
public class waypoints : MonoBehaviour{
public static Transform[] points;
void Awake()
{
points = new Transform[transform.childCount];
for (int i = 0; i < points.Length; i++)
{
points[i] = transform.GetChild(i);
}
}
}