I wanna make enemy loop back to the first waypoints after it finished its patrol path. But somehow it stopped at the first waypoint. Basically didn’t even complete the first cycle. This is my code:
public class RedAlienMovement : MonoBehaviour
{
private Waypoints wPoints;
private int wPointsIndex;
[SerializeField] private float speed;
void Start()
{
wPoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, wPoints.waypoints[0].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, wPoints.waypoints[wPointsIndex].position) < 0.1f)
{
if (wPointsIndex < wPoints.waypoints.Length - 1)
{
wPointsIndex++;
}
else
{
wPointsIndex = 0;
}
}
}