I’m trying to make it so a script spawns parts in an area of where another part is. And no I can’t use coordinates as I’m trying to spawn parts in a cylinder/ball shape radius. But how do you spawn parts in a radius of a part?
Is it going to be at a random position within the ball or sphere that the part will spawn? Or is it just in the center?
I’m trying to make it spawn at a random pos
And nothing from the part can stick outside the sphere/cylinder?
yeah I’m trying to make it stick inside.
Also one more question:
Will the part spawn with a set rotation? (0,0,0) for instance
Or will it spawn with a random rotation?
I can show an image if you need a reference, but Roblox Studio is still updating for me.
The part gotta start at 0,0,0 rotation
local function getRandomPositionInPart(part: BasePart) : Vector3
local origin = part.Position
local maxX = part.Size.X/2
local maxY = part.Size.Y/2
local maxZ = part.Size.Z/2
local randomPos = Vector3.new(
math.random(-maxX, maxX),
math.random(-maxY, maxY),
math.random(-maxZ, maxZ)
)
return origin + randomPos
end
hope this helps
It doesn’t since the height of the sphere changes the x and y bounds.
As defined by x^2 + y^2 + z^2 = r^2
this one spawns parts in a cube
i found this one for a sphere
Ok so this is going to involve some math here, but we can do it.
(ignore 1st step for cylinders… it will just be a random position between the height and the lowest points of the cylinder)
- We need to determine the bounds that the part can be placed (for y)
A sphere is defined by x^2 + y^2 + z^2 = r^2. Since there are 3 variables, this would be hard to define the max and min that the part can be placed.
HOWEVER, take a look at the part that will be placed: Which side is longer? The length (x) or width (y)? The shorter side? Set it in the equation as 0. (so if the width is the shorter side, then set the equation to be x^2 + z^2 = r^2)
After, get the positions of the edges of the long side of the part (so if the length of the longer side, you would take HALF of the x coordinate’s size and make it positive and negative to get 2 values.)
Get the sphere’s radius by taking half of any coordinate of its Size.
Just plug in to the equation to get z (add half of the part’s (that will be placed) z coordinate for size if it spawns below the halfway mark of the sphere, but subtract if it spawns above halfway mark).
- Then, you need to get a random x and z position.
Now that you have a random y position, plug in to the equation x^2 + y^2 + z^2 = r^2.
It should give you a constant, for which it will be x^2 + z^2 = c.
Using the longer side (length for me, which is x), subtract HALF of the spawning part’s longest side from what the MAX length can be, and add for the MIN. Then, spawn a random x value.
- y position
x^2 + y^2 + z^2 =r^2
x and z are now set, so it would be y^2 = c
solve for y then
I can program it if you want.
Was never specified that it was a sphere?
Sorry I completely forgot to mention, I don’t want the Y radius as I’m trying to make it spawn parts along the X Z axis only, and the Y axis is centered along the center of the spawning part. Very sorry for not mentioning that.
I’m trying to spawn parts in a cylinder/ball shape radius.
That’s what OP wanted.
oh sorry, i didnt notice charrsss
Well, my instructions wouldn’t be changed that much, as all you are doing is setting y as a constant.
To be honest with you, you are quite lucky that this doesn’t require calculus lol
I’m having a bit of trouble trying to read this could you make it a bit shorter
So you would have a set Y value.
Sphere equation: x^2 + y^2 + z^2 = r^2
Get the radius of the sphere by taking half of any Size coordinate (x, y, or z). This should be constant.
Subtract y^2 from both sides to get x^2 + z^2 = r^2 - y^2, with the left side having a numerical value.
Then, which Size coordinate is longer? x or z?
If x is longer, ignore z (for now). Get HALF of the Size coordinate (of the spawned part) for x, and subtract it from the POSITIVE x value (when you solve the equation x^2 = r^2 -y^2) for the MAX bound for randomness. Do the same with the NEGATIVE x value (when solving the equation) by ADDING HALF of the Size coordinate (of the spawned part) for x to get the MINIMUM bound for randomness.
If z is longer, replace what I said for x and put in z (and vice versa for replacing z’s with x’s).
After generating a random x (or z, but I’ll use x here as it’s easier for me) value, you should have it as z^2 = r^2 - y^2 - x^2. When solving for z (or x if it is the shorter side), you should get 2 values: one positive and one negative. Take the spawning part’s z component for Size and half it. Add it to the negative z value, and subtract it for the positive. Those are your minimum and maximum values for the z axis, respectively.
Sorry, this is really complex, and I don’t think I can make it shorter than this.
Yeah its very complex and like I get the first part but I lose myself on the 2nd and 3rd so unless you can program it for me I’m gonna be trying out ways I can do this myself (No idea why 2nd and 3rd is so confusing to me)