i am experimenting with a part placement script, it works alright so far, here is what the script looks like so far:
local function randomPointOnSphere(center, radius)
local randomX = (math.random()*2)-1
local randomY = (math.random()*2)-1
local randomZ = (math.random()*2)-1
local randomVector = Vector3.new(randomX, randomY, randomZ)
local randomDirection = randomVector.Unit
local randomDirectionWithRadius = randomDirection * radius
local randomPointOnSurface = center + randomDirectionWithRadius
-- Adjust the randomPointOnSurface to prevent clipping inside the planet
local distanceToSurface = (randomPointOnSurface - center).Magnitude - radius
randomPointOnSurface = randomPointOnSurface - randomDirectionWithRadius.Unit * distanceToSurface
return randomPointOnSurface, randomDirection -- Return the random direction as well
end
local function spawnPartsAroundSphere(spherePart, numParts)
local radius = spherePart.Size.X / 2
local center = spherePart.Position
for i = 1, numParts do
local randomPoint, randomDirection = randomPointOnSphere(center, radius) -- Get the random direction from the randomPointOnSphere function
local part = game.ReplicatedStorage.Core:Clone()
part.Position = randomPoint
part.Parent = workspace
part.Anchored = true
-- Rotate the part to face towards the center of the sphere
part.CFrame = CFrame.lookAt(part.Position, center)
end
end
spawnPartsAroundSphere(game.Workspace.Planet, 1000)
but some parts are inside the sphere more than others, i am a bit confused, how would i solved this? so far i tried upvector, which produced a very odd result.
lookvector -10 looks fine right?
its like the whole sphere cubes got offset uniformally