This is what it looks like when I fire in a straight line for a while, to achieve bullet spread, Im using a open-sourced function that generates a cone to achieve cone-shaped bullet spread.
local actualBulletDirection = (mousePosition - muzzlePosition).Unit
local bulletDirection = generateRandomCone(actualBulletDirection, math.rad(tool:GetAttribute("BulletSpread"))) * tool:GetAttribute("BulletTravelDistance")
local function generateRandomCone(direction: Vector3, coneAngle: number): Vector3
local cosineOfAngle = math.cos(coneAngle)
local randomZ = 1 - math.random() * (1 - cosineOfAngle)
local randomPhi = math.random() * math.pi * 2
local radius = math.sqrt(1 - randomZ * randomZ)
local randomX = radius * math.cos(randomPhi)
local randomY = radius * math.sin(randomPhi)
local randomVector = Vector3.new(randomX, randomY, randomZ)
if direction.Z > 0.9999 then
return randomVector
elseif direction.Z < -0.9999 then
return -randomVector
end
local orthogonalAxis = Vector3.zAxis:Cross(direction)
local rotationAngle = math.acos(direction:Dot(Vector3.zAxis))
return CFrame.fromAxisAngle(orthogonalAxis, rotationAngle) * randomVector
end
return generateRandomCone
and @RobloxHasTalentR but what if you want a wide spread angle over longer distances?
If you have multiple targets in sight and offset instead of use a cone then close up players might get hit and farther away characters might get missed.
There’s a reason it’s called a spread. It’s a cone, not a straight line offset square box.
Yeah, that was my goal. Any ideas on what im doing wrong? I definitely know it has to do with the way im using the function, not the cone generator function itself. Because I did a test with the function just to see if it worked
Okay! I figured it out, I noticed that when I kept firing, turns out, the raycast was hitting the part visualizer that I was making, nothing wrong with the code I showed.