❔ 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]
11 Replies
Saber
Saber2y ago
well instead of going to a random spot, make it go to the next spot
RuneShiStorm
RuneShiStormOP2y ago
Yes, HAha, thats what I want, But I really dont know how :S Im in the Beginner forum. right? 😛
Saber
Saber2y ago
keep track of which spot its at, and increment it once the wait time is complete
RuneShiStorm
RuneShiStormOP2y ago
I deleted all Random events and put [1] instead of random. That way he walked to spot 1. But then he stops there cuz he dont know where to go next what kind of "function should I google for that?" Is it a Transform [] thing or what am I looking for?
Saber
Saber2y ago
realistically your not going to google to find something to spoonfeed you this. You're going to have to figure out how to make the logic for it, theres not much your going to have to change to get this to work as expected
RuneShiStorm
RuneShiStormOP2y ago
I found something like this. destPoint = (destPoint + 1) % points.Length; Seem to be what Im looking for
Angius
Angius2y ago
Just... save the index of the current point Then you can go to that point + 1
RuneShiStorm
RuneShiStormOP2y ago
Aigh, Index is the "[ ]" symbols?
Angius
Angius2y ago
It's a number [] is the indexer array[index] That's how you get an item from an array Instead of using a random value, use some index Then increment that index So next time you access the array, you'll take the next item
RuneShiStorm
RuneShiStormOP2y ago
Ah, I think that make sense, thank you! At least I know what to read up about to get it to work!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server