Generating a random position 8 studs away from another

Im trying to do what the title says. I have a target position and what Im trying to do is generate a position 8 studs away from it in the x,z direction. So imagine a circle with a radius of 8 studs and I’m trying to get a random point on that circle.

local direction = CFrame.Angles(math.rad(math.random(0,90)), 0, math.rad(math.random(0,90)))|
clangorFight.Clangor.Humanoid:MoveTo(getGoalPosition() + direction*8)|

The error I’m getting from this is
invalid argument #2 (Vector3 expected, got number)

1 Like

It seems like the error is from 0. It is expecting a vector 3 value with 3 numbers. You can make one like this:

Vector3.new(value1, value2, value3)

I’m not sure what 0 represents, but I think you must put a vector 3 value there instead.

Hope this helps.

1 Like

You should be using :PivotTo() instead of :MoveTo()

:PivotTo will take a CFrame (which I see ‘direction’ is)
but when you do getGoalPosition() + direction, if getGoalPosition is returning a CFrame, you can’t add a CFrame + a CFrame, it wants the second value to get a Vector3

CFrame + Vector3 is what it needs, but I think you are doing CFrame + CFrame

1 Like

getGoalPosition returns a vector3 value.

Doesnt it need vector3 + vector3? Thats what im trying to do because moveTo uses position

oh oh, lol, my bad, yeah, MoveTo, change your direction to direction.Position, to make it a vector3, because as is, direction is holding a cframe

try this

local startPosition = getGoalPosition()
local directionCFrame = CFrame.Angles(math.rad(math.random(0,90)),0,math.rad(math.random(0,90)))
local endPosition = startPosition + (directionCFrame.LookVector*8)
1 Like

if this is for a humanoid to walk to a position, you might be working with a flat surface, or floor, so maybe you are wanting to only rotate around the Y axis

such as

local directionCFrame = Frame.Angles(0,math.rad(math.random(0,90)),0)

or something


Im getting half circle shapes, but on the wrong axis

I think you need to rotate on the Y, and not just 90, but try 360 for random angle.

1 Like


it works

1 Like

if you want to get the exact opposite spot on the circle, you can take do…

local oppositePosition = startPosition + (- directionCFrame.LookVector*8)

take the negative of the lookVector, and multiply that by 8

1 Like

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