Finding a random spot

When all paths to my NPCs target is blocked, I would like to send the NPC roaming the area around.
How can I pick a random spot close to the target?
I tried using GetPointsAroundPoint, but didn’t get it to work - prob because of low programming skills :wink:
A small example (in javascript) would be nice. Thanks :slight_smile:

My test script:
Trying to fill an array with possible points

var myPoints:Vector3[];
var myList:Vector3[];
function getPoints() {
myPoints = Pathfinding.PathUtilities.GetPointsAroundPoint(Vector3(0,0,0),[0], myList, 10,1);


The better option is to use a RandomPath.

A RandomPath will move some specified distance to a nearby node.

To actually pick a random point near the NPC using that API, you would have to first use a ConstantPath to get some nodes around the NPC, then use the GetPointsOnNodes method to get a random point on it.
This very complicated for just picking a random point, so using the RandomPath is recommended.

Thanks for the quick reply!
Sounds interessing, but my NPC will probably move the in the oposite direction of it intended target (goal) - leaving it roaming “forever”. But it may work satisfying…I’ll test it :slight_smile:

Is it possible find objects with specific tags that are blocking a path? It would solve my NPC “problem” - helping it in the right direction where it can destroy the blocking object :wink:

I can’t get RandomPath to work. Can it be because I got the free version or missing some files? Checked assests - under “Pathfinders” I only found the ABPath file. Should I have RandomPath here as well?

The normal path is working fine.

Anyway, here is how I get the normal path and trying to get the random path.
Unity sais: “The name ‘RandomPath’ does not denote a valid type (‘not found’). Did you mean ‘UnityEngine.RuntimePlatform’?” (referes to the line marked *)

import Pathfinding;
private var seeker:Seeker;
/** Call this function to reset the path and get a new path */
private var path:Path;
private var newPosition:Vector3;
function getNewPath(newPosition) {
path = null; // Reset the old path before getting a new
seeker.StartPath (transform.position,newPosition, OnPathComplete);

private var randompath:RandomPath; *
function getRandomPath() {
path = null; // Reset previous/normal path before getting the random path
randompath = new RandomPath(transform.position,10);


Yes, the RandomPath is only available in the pro version.

For the second issue, the RandomPath has a variable called “aim” and one called “aimStrength”, these can be used to make the choice biased, so if you set “aim” to whatever point you want it to reach, and set “aimStrength” to 0.5, it will move towards that point, but it will still have some randomness in it.


The code you wrote for getting a random path is correct, it’s just that you have the free version and not the pro version. The only thing worth commenting on is the G-score you used. A rule of thumb is that the G score to reach a specific node is about 1000 times the distance to it, so you generally want to specify g scores with values around 10000 or more.

Ok. Thanks again.
Since this is a solo project and mostly for fun the free version was the right choise, but I’ll think about upgrading :wink:


What will probably work very well is actually just picking a random point without any check for if it is on the graph or not, and then just use a normal ABPath to move to it.
Since the system will automatically select the closest node on the graph regardless of what point you tell it to move to, this will work well, but it will be slightly biased towards nodes near obstacles.

I was thinking the same thing :wink:
Is it possible to search for preferred locations/areas/points when getting a path? i.e. sticking to roads etc


You can use to set roads to have a specific tag. Then on your seekers you can set the penalties for walking on different tags. In this case you would want to set the default tag’s penalty to something very large (10000 maybe) and leave the road’s penalty at 0.

Thanks - that worked fine :slight_smile: