Help with radius AI spawn system

Hello! I am working on making an AI spawn script, in this script I want to get the HumanoidRootPart of the character and then I want to use that position to spawn an AI randomly in the radius of 100 studs near the character. The only thing I can think of is using pi, how could I do this?

So I believe you want an AI to spawn anywhere in a radius of 100 around the humanoidRootPart.
I think this should probably work.

So you will first have to select a direction from 1 degrees to 360 degrees and rotate it.
Then you can choose any number from 1 to 100 and move it front that much.

Here is an example script-

local humanoidRootPart = -- wherever it is located
local AI = -- wherever it is located

local direction = math.random(1, 360)
local magnitude = math.random(1, 100) -- this is just a distance value i.e. how far away from the player

AI:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.Angles(0, direction, 0)
AI:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.new(0, 0, -magnitude)

You can choose to move it using any method, but it has to be relative to the humanoid root part

1 Like

It works! However, it seems to sometimes spawn on the player or really close by, could there be a way to maybe make it so that it will not spawn within _studs of the player and then do the math.random?

You could change the

local magnitude = math.random(1, 100)

to

local magnitude = math.random(40, 100)

the 40 is minimum distance, 100 is maximum

1 Like