Youâre saying âradiusâ, but I think you want a cone. Radius isnât quite specific enough in this context.
i.e. if your LookVector is the black line in this image, you want to pick some velocity in the red cone:
I borrowed that image from this stackoverflow answer.
Hereâs the top answer on that question translated into lua:
-- axis: center line of cone
-- angle: angle from center to edge of cone
-- returns: a random unit vector inside the cone, evenly distributed along the partial surface of a unit sphere.
local function RandomCone(axis: Vector3, angle: number)
local cosAngle = math.cos(angle)
local z = 1 - math.random()*(1 - cosAngle)
local phi = math.random()*math.pi*2
local r = math.sqrt(1 - z*z)
local x = r * math.cos(phi)
local y = r * math.sin(phi)
local vec = Vector3.new(x, y, z)
if axis.Z > 0.9999 then
return vec
elseif axis.Z < -0.9999 then
return -vec
end
local orth = Vector3.zAxis:Cross(axis)
local rot = math.acos(axis:Dot(Vector3.zAxis))
return CFrame.fromAxisAngle(orth, rot) * vec
end
Youâd use it like
-- angle of the cone, i.e. the largest angle off the firing axis you could be
local SPREAD = math.rad(45)
-- speed of the bullet (can also be random if you want)
local SPEED = 100
part.AssemblyLinearVelocity = RandomCone(script.Parent.CFrame.LookVector, SPREAD) * SPEED
Havenât tested this yet. If this is not what youâre looking for please elaborate a bit more