How do i make raycast shotgun spread?

Hello, i’ve got a problem, for some reason my shotgun spread doesn’t work and i need to fix it, is there any way to fix this?

for i = 1, Setting.BulletsWhenShot do
	CurrentAmmo = CurrentAmmo - 1
	Players.LocalPlayer.PlayerGui.GunUI.Weapon.Ammo.Text = CurrentAmmo .. "/" .. Setting.AmmoInMag
	ShotAnim:Play()
	local hitPart, hitPosition	= workspace:FindPartOnRay(Ray.new(script.Parent.Shoot.CFrame.p, (Mouse.Hit.p - script.Parent.Shoot.CFrame.p).Unit * 999), Players.LocalPlayer.Character)
	script.Parent.Shot:FireServer(script.Parent.Shoot.CFrame.Position,  hitPosition, script.Parent.Handle.Rem, SecurityKey, hitPart, TKEnabled, Setting.Damage)
	workspace.CurrentCamera.FieldOfView = Setting.FOVwhenshoot
	TS:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {FieldOfView = Setting.zoomFOV}):Play()
end

But it’s shooting like a normal weapon, not like a shotgun
https://gyazo.com/f8147fc0ff82c5995abd25830aac4098

Help please :pray:

1 Like

Two major issues, with every shot multiple bullets will be used and also you don’t have any randomness in the raycast. should probably create a cframe them use cframe.angles to change it in a random direction so it’s not so lasery

2 Likes

I personally would add a random number to achieve this spreading effect

adding a random number to the value wouldn’t work because it’s a look vector type value and no matter what part of the sum you change you will get some janky unexpected results. For example if you just change the unit by adding on a random v3 the ray could go backwards instead of fowards. If you change the mouse.hit.p by adding on a random v3 then it’s spread would get smaller the further away you shoot.
That’s why you have to make it into a CFrame then change it using CFrame.Angles

1 Like

where should i add a randomness here? Because i attempt to do that, but it was working kinda weird i think?

local hitPart, hitPosition	= workspace:FindPartOnRay(Ray.new(script.Parent.Shoot.CFrame.p, (Mouse.Hit.p - script.Parent.Shoot.CFrame.p).Unit * 999), Players.LocalPlayer.Character)
2 Likes