I’ve started making a tower defence game and I’m having a problem with the enemy AI as for whatever reason It is going straight to the end and not going to the path I created. I assume that it is because I’m going off a fairly old Brackeys video and not using pathfinding. I’ve posted the code I’m using down below. If you have any suggestions or know how to fix the problem i’m having that would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public float speed = 10f;
private Vector3 vector3;
public Transform target;
private int wavepointIndex = 0;
void Start()
{
target = Waypoints1.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.2f)
{
GetNextWaypoint();
} }
void GetNextWaypoint()
{
if (wavepointIndex >= Waypoints1.points.Length - 1)
{
Destroy(gameObject);
}
wavepointIndex++;
target = Waypoints1.points[wavepointIndex];
}
}