Bullet starts rotating as I fire?

I can’t figure out why my bullets keep rotating so weirdly when im firing?


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
1 Like

Why would you make a cone and not just add a slight offset to the xyz position of the bullets CFrame when you create it?


Also your formatting is a little poor. Please spread your code out a little bit, it will make it a little easier to read. Squashed code is bad code.

Like the guy suggested above you can add the bullet offset and that to fix offset on random

local actualBulletDirection = (mousePosition - muzzlePosition).Unit
local bulletSpread = tool:GetAttribute("BulletSpread")
local bulletTravelDistance = tool:GetAttribute("BulletTravelDistance")

local function generateRandomOffset(spread: number): Vector3
    local offsetX = (math.random() - 0.5) * 2 * spread
    local offsetY = (math.random() - 0.5) * 2 * spread
    local offsetZ = (math.random() - 0.5) * 2 * spread
    return Vector3.new(offsetX, offsetY, offsetZ)
end

local function getBulletDirection()
    local randomOffset = generateRandomOffset(bulletSpread)
    local newDirection = (actualBulletDirection + randomOffset).Unit
    return newDirection * bulletTravelDistance
end

local function shootBullet()
    local bullet = Instance.new("Part")
    bullet.Size = Vector3.new(0.2, 0.2, 2)
    bullet.CFrame = CFrame.new(muzzlePosition, muzzlePosition + getBulletDirection())
    bullet.Velocity = getBulletDirection()
    bullet.Parent = workspace
end

shootBullet()

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.

1 Like

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.

1 Like