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

How would I clone a part to a random position inside another part?

Asked by 4 years ago

How would I clone a part to a random position inside another part? I have tried different approaches to this problem, but the part always happens to appear in the middle of the part it needed to appear in.

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Okay, so this one'll need a liiiittle bit of work: You want your position to be within the part, so assuming they're not meshparts, unions, or anything else that would change the meaning of "inside" it, you may want to do something similar to this:

--NOTE: Untested
local PartA,PartB = --...
local MaxDisplacement=(PartA.Size-PartB.Size)/2
PartB.CFrame=PartA.CFrame * CFrame.new( --Goto A's CFrame, and displace by random xyz
    -MaxDisplacement.X+math.random()*MaxDisplacement.X*2,
    -MaxDisplacement.Y+math.random()*MaxDisplacement.Y*2,
    -MaxDisplacement.Z+math.random()*MaxDisplacement.Z*2
)
Ad

Answer this question