Walking along some waypoints in a random way


I have basicly a shop which I want my customers to walk in and follow some way-points in a random way, so that they don’t all follow each other in the same pattern. What would the best way of achieving this?


To make the selection the most uniform, I would suggest simply moving the target to a new random position when the character gets close enough to the current target. You could also use a RandomPath, however depending on your graph layout, that may not distribute the customers evenly in the shop.

I am using a grid graph. Is there an example of using a Random Path ? I have seen that there is one in the examples but i could do with just an example that has randomPath only code.

So here is my shop which i want the customers to all GOTO the blue dots at some point and pause using a random path.
They come in from the entrance point and exit from the exit point.


There is an example of using a random path here: http://arongranberg.com/astar/docs/class_pathfinding_1_1_random_path.php

However for your use case, I would recommend just moving the target point around randomly as that will spread them out more uniformly.

OK thats good…

There is a question about the Paths … Say i give the customer a path and he finishes it, how do i detect that the customer has finished that current path and how do you give him a new one ?

Do you have to measure how far from the end of the path he is then create a new path object like this
//Create a path object
RandomPath path = RandomPath.Construct (transform.position, theGScoreToStopAt);

So you are saying that the random path wouldent be the best way to go but to change the position of the target to random places ? Is there a way to pick a random place ?


That depends on the movement script that you are using.
For example the AIPath script will call the OnTargetReached method which you can override to run some code to move the target.

There are some functions to pick a random point on a collection of nodes, but for this purpose I would just define the size of the store and do something like

var point = new Vector3(Random.value*storeSize, 0, Random.value*storeSize);

Possibly added to an offset in case the store does not have a corner in the world origin. Also I am not sure if you are using 2D in the XY plane or the XZ plane.

1 Like

OK i got this working nice now… To get the customers not to bump into one another and not to stand on top on each other do i need the avoidance script ?


Yes, that is correct.

OK this is my script so far

`using UnityEngine;
using System.Collections;
using System;

public class CustomerPath : AILerp {

private Transform[] wayPoints;
private Transform homePoint;
private int nextPosition = 0;

private int pathWidth = 5;
private int pathHeight = 5;

void Start()

	GameObject waypointParent = GameObject.FindWithTag("WayPoints");
	GameObject homePoint = GameObject.FindGameObjectWithTag("HomePoint");

	wayPoints = new Transform[waypointParent.transform.childCount];

	for(int i = 0; i < waypointParent.transform.childCount; i++)
		wayPoints[i] = waypointParent.transform.GetChild(i).transform;

	///randomize the array

	Array.Resize(ref wayPoints, waypointParent.transform.childCount + 1);
	wayPoints[wayPoints.Length -1] = homePoint.transform;

	seeker.StartPath(transform.position, wayPoints[nextPosition].position, OnPathComplete);


public override void OnTargetReached ()

	if(nextPosition >= wayPoints.Length)



IEnumerator PauseAtShelf()
	yield return new WaitForSeconds(3);

	seeker.StartPath(transform.position, wayPoints[nextPosition].position, OnPathComplete);


Whats the best way of adding the Avoidance ?


Ah, you are building it on top of AILerp.
Since AILerp is designed to follow the path exactly, it does not work well with local avoidance since that is designed for the purpose of deviating from the path.

Well I build it on top of AILerp because I want it to use the 2d … Would i have to use a customized AI path script for this ?


Yes, I am afraid you will. Sorry for not including support for 2D in the other movement scripts.
Or you can try writing a custom simple movement script (see the get started tutorial).