Try creating an union to make a circular wall around the circle and use Raycast to send a ray to every direction to detect if they can walk to a certain position or not. By the way you can make the union invisible.
Getting a random point in a circle involves a bit of trigonometry…
We know that in a circle, the following is true
-- Where x and y make up an edge point on the circle
cos(theta) * radius = x
sin(theta) * radius = y
In order to get a random point within the circle, we need to get a random theta value between 0 and 2 * math.pi, and we also need to get a random radius value between 0 and radius.
We also need to account for a bias where more “random” points tend to end up closer towards the center of the circle. You can read more about that on this thread…
Here is the code I wrote for you
function GetRandomPointInCircle(circle)
assert( typeof(circle) == "Part", " circle must be a part ")
local radius = circle.Size.Z / 2
local cf = circle.CFrame
local theta = 2 * math.pi * math.random()
local r = radius * math.sqrt( math.random() )
return cf.Position + cf.RightVector * math.cos(theta) * r + cf.UpVector * math.sin(theta) * r
end
After finding the random point, im sure you can figure out the rest yourself…
I second this post. I was actually just now reading that stack overflow page to write an answer. Funny to see someone else was thinking the same thing!
Are the things in parenthesis just for reference? Also, how do I get the random angle? Sorry if I sound dumb, I don’t normally do much with math when scripting.
Do I just get a random number between 1 and 360 or something like that
I tried both @Xx1Luffy1xX’s and @comsurg’s solutions but for some reason they don’t work. I’m not sure what the problem is, but the character just seems to move to the center and then just kinda stays there.
function GetRandomPointInCircle(circle)
-- the notes are just prints so I could try and find the problem
local radius = circle.Size.X / 2
--print(math.random(1,360))
local randomAngle = math.rad(math.random(1,360))
--print(randomAngle)
local randomNum = math.random()
--print(randomNum)
local sine = math.sin(randomAngle) * randomNum * radius
local cosine = math.cos(randomAngle) * randomNum * radius
--print(sine, cosine)
local finalPoint = Vector3.new(cosine, 0, sine) + circle.Position
return finalPoint
end
local newPoint
while true do
newPoint = GetRandomPointInCircle(workspace.Circle)
print(newPoint)
script.Parent.Humanoid:MoveTo(newPoint)
print("moving")
script.Parent.Humanoid.MoveToFinished:Wait()
print("moved")
end
This is actually because the cylinder, which you used to make the circle, has its localized X axis basis vector facing away from the circle. Here is the fix…
function GetRandomPointInCircle(circle)
local radius = circle.Size.Z / 2
local cf = circle.CFrame
local theta = 2 * math.pi * math.random()
local r = radius * math.sqrt( math.random() )
return cf.Position + cf.LookVector * math.cos(theta) * r + cf.UpVector * math.sin(theta) * r
end
local newPoint
while true do
newPoint = GetRandomPointInCircle(workspace.Circle)
print(newPoint)
script.Parent.Humanoid:MoveTo(newPoint)
print("moving")
script.Parent.Humanoid.MoveToFinished:Wait()
print("moved")
end