❔ How to make it NOT random and follow a Transform[]
Hello, new here and hope to get some help/pointers on what to do here.
I have made a script where my NPC can walk to different positions and wait for a sec, before moving on. But it walks to RANDOM positions... I can't find ANY tutorial on how to make an NPC go to each spot in order. And wait...
This is my Script:
[CODE]
public class patrolScript : MonoBehaviour
{
public float speed;
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;
}
}
}
}
[/CODE]
I have made a script where my NPC can walk to different positions and wait for a sec, before moving on. But it walks to RANDOM positions... I can't find ANY tutorial on how to make an NPC go to each spot in order. And wait...
This is my Script:
[CODE]
public class patrolScript : MonoBehaviour
{
public float speed;
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;
}
}
}
}
[/CODE]