Unable to spread bullet raycast

i have a raycast gun but the only problem is that it doesn’t spread, i set up a random offset in my code but not sure if that does anything.



local Offset = Vector3.new(
		math.random(-100,100),
		math.random(-100,100),
		math.random(-100,100)
	)/100 * 0.2
	
	local raycastParams = RaycastParams.new() -- RaycastParams
	raycastParams.FilterDescendantsInstances = {c,workspace.CurrentCamera}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist 
		
	local direction = (mouse.Hit.p - GunHandle.Position).Unit  + Offset
	
	local RayCastResult = workspace:Raycast(GunHandle.Position, direction  * 300, raycastParams) -- Raycast
	local Intersection = RayCastResult and RayCastResult.Position or  GunHandle.Position + direction * 300
	local distanceToWall = (Intersection - c.HumanoidRootPart.Position ).Magnitude    -- check if raycast hit

	local Bullet = game.ReplicatedStorage.Part:Clone()
	Bullet.Parent = workspace.CurrentCamera
	Bullet.CFrame = CFrame.new(GunHandle.CFrame.p,mouse.Hit.p) * CFrame.new(0,0,-4)`Preformatted text`

Although your raycast has spread, the bullet CFrame code does not. Use the direction within the CFrame code and not mouse Hit .P which will always make it go towards the mouse.

local bulletPos = GunHandle.CFrame.p
Bullet.CFrame = CFrame.new(bulletPos, bulletPos  + direction) * CFrame.new(0,0,-4)
2 Likes