Random point in an area (excluding some parts)

Yeah basically how do i pick a random point in an area for an npcs to path find to, but excluding some tables and such cause i dont want them to stand on it or fail pathfinding. also some zones will be excluded as well. (that was horrible phrasing i apologise)

1 Like

This code returns a random direction with random radius until a specified radius

function randomDirInRadius(radius: number): Vector3
  local angle = math.random() * math.pi*2
  radius = math.random() * radius

  return Vector3.new(
    math.cos(angle) * radius, 0,
    math.sin(angle) * radius
  )
end

but it works only for XZ plane

why radiuses though, surely that would be too complicated for a simple idle around an allowed area, and how would i get the specified radiuses

also im confused on how you use this function

1 Like

excluding some tables and such

You’ll have to explain what this means. Do you have every point already pre-selected and your goal is to simply move between pre-selected points randomly?
Give us some better details on how you have your data set up.


A box would be marginally more complicated.

local function randomPointInBoxCentered(x: number, z: number): Vector3
	return Vector3.new(
		math.random() * x - x / 2, -- Convert to range [-x/2, x/2)
		0,
		math.random() * z - z / 2
	)
end

You measure and/or guess the radius of the area you need to be covered. If your area is relatively square or circular, it works fine.

You add it to the origin (center point) of your area. Such as,

NPC:MoveTo(Origin + randomDirInRadius(50))

theres no pre selected points its literally that i have an area with some obstacles, npcs cannot leave that area or collide with obstacles. they must pick a random point in the area and pathfind to it and not fail a pathfind by trying to pathfind into an obstacle. i dont know how to make a function to pick a random point efficiently

The answer above gives random coordinates inside a part, the “area” is a part i presume.
Just make it generate random point using that function and create a path to that point, if no viable path emerges generate another point and keep trying.

To exclude characters from going on furniture you can make hitboxes and assign a PathfindingModifier to them and assign a big cost to these hitboxes so that pathfinding avoids going over them.

I suggest you look into Dijkstra algorithm. Basically, it’s an algorithm used in pathfinding. It starts from your starting position (or your NPC position) and expands like a square or circle without going through wall, and it keeps expanding until it hits the goal position. In this case, you can tweak it to make it expand to only some studs, stop, then choose a random point in the available list, and then backtrack form where it comes to get path. Just take a look at its visualization being used in path find and you’ll get what I mean.

But the overall best approach is to manually collect all the positions that the NPC can possibly find a path to, pick a random position from all those positions, and just path find from there.

Here’s what Dijkstra should looks like: https://youtu.be/gsl5DQkA9Fk?si=J483_Tay9EvPnr-1

So you want a random position within a specific set of coords? Would this function be okay?

function PickAreaWithinBounds(xmin, xmax, ymin, ymax, zmin, zmax)
    return Vector3.new(math.random(xmin, xmax), math.random(ymin, ymax), math.random(zmin, zmax))
end

To pick your random point, call the function supplying the bounds of the area (Defined as minimum and maximum ranges in X, Y and Z coordinates)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.