I’m a beginning developer and I followed Blackthornprod’s ‘PATROL AI WITH UNITY AND C# - EASY TUTORIAL’ for one of my projects to understand a bit more about AI, but I need the enemy to go to the next gameobject in the array, instead of it being random. I can’t find any answers online and I need help, here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patroll : MonoBehaviour
{
public float speed = 5f;
private float waitTime;
public float startWaitTime;
public Transform[] moveSpots;
private int randomSpot;
// Start is called before the first frame update
void Start()
{
waitTime = startWaitTime;
randomSpot = Random.Range(0, moveSpots.Length);
}
// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
if(Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2f)
{
if(waitTime <= 0)
{
randomSpot = Random.Range(0, moveSpots.Length);
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
I think it’s something got to do with ‘randomSpot = Random.Range(0, moveSpots.Length)’