Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to ensure a part clone is not in a custom set radius using magnitude?

Asked by 5 years ago

I am trying to spawn parts with a magnitude of 50 from a part, the center. How do I go about doing this??

main = workspace.Enemy
tower = workspace.Tower
spawnradius = 50
while true do
    part = main:Clone()
    part.BrickColor = BrickColor.Random()
    mag = (part.Position - tower.Position).magnitude
    if mag <50 then
        -- I have no idea what to do.
    end
end

I've thought about trying to understand how Roblox calculates magnitude but it has to do with something on absolute values. But being a 13-year-old, I have no idea how to find that. Any help would be appreciated. Solutions don't have to only rely on magnitudes.

0
This might prove useful in just understanding magnitude: https://forum.scriptinghelpers.org/topic/763/how-magnitude-works-in-layman-s-terms User#26971 0 — 5y
0
Thanks but it's shows errors. Lol. FadedJayden_Dev 118 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You could use CFrames to accomplish this since CFrame is in object (local) space, so if you're referencing a point with the position (25,50,0) you're actually referencing the point relative to the corresponding axes; 25 studs in the (object's) X axis, 50 studs in the (object's) Y axis and 0 studs in the (object's) Z axis.

math.randomseed(tick())
part.CFrame = CFrame.new(0,0,0) * CFrame.Angles(math.rad(math.random(0,360) - 180),math.rad(math.random(0,360) - 180),math.rad(math.random(0,360) - 180)) -- set random rotation
part.CFrame = part.CFrame * CFrame.new(math.random(50,75),0,0) -- move 50-75 studs "forward"
part.CFrame = CFrame.new(part.CFrame.Position) * CFrame.Angles(0,0,0) -- reset rotation
1
Hmmmm it works but not the results I am trying to achieve. I want parts to spawn outside a circle. In the middle of the circle is a part. So I changed line 3 to *CFrame.new(math.random(50,75),0,math.random(50,75)) But I am getting a half circle instead of a full circle. I need a way to store the negative values too. FadedJayden_Dev 118 — 5y
0
It should work as you described now. Vinceberget 1420 — 5y
1
Somewhat works! I'll just try to tweak to fix the issues. Thanks! FadedJayden_Dev 118 — 5y
Ad

Answer this question