© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
20 replies
RuneShiStorm

❔ 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]
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How do i make a random a static?
C#CC# / help
3y ago
How to make Random Password Generator [Answered]
C#CC# / help
4y ago
❔ How to make instantiated object move using transform function?
C#CC# / help
4y ago
❔ How to make instantiated object move with transform function?
C#CC# / help
4y ago