patrol enemy unity 3d code example
Example: how to make a patroling enemy in c#
public float speed;
public float startWaitTime;
public Transform[] moveSpots;
private float waitTime;
private int randomSpot;
void Start()
{
randomSpot = Random.Range(0, moveSpots.Length);
waitTime = startWaitTime;
}
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;
}
}
}