Shotgun's off-centered bullet spread

I was making a raycasting shotgun and i found out that the bullets are off-centered.
And it’s probably because of this line :

local RaycastResult = workspace:Raycast(Origin, Direction.LookVector * 150, RayParam)

but im not good with CFrame and stuff, so any help will be appreciated.

screenshots :



localscript :

local UIS = game:GetService("UserInputService")
local RPS = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Animator = Character.ViewModel.AnimationController.Animator

local FireEvent = RPS.Events.RemoteEvents.FireGun

local DefaultGun_Folder = RPS.Animations.Default.Gun
local DefaultGun_Shot = DefaultGun_Folder.DefaultGun_Shot

local GunShotAnim = Animator:LoadAnimation(DefaultGun_Shot)

local BulletsCount = 8
local Spread = 4
local Cooldown = 0.75

local Canrun = true

UIS.InputBegan:Connect(function (input, gpe)
	if gpe then return end
	
	local Origin = workspace.CurrentCamera.CFrame.Position
	local Dir = workspace.CurrentCamera.CFrame.LookVector * 150
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Canrun == true then
		print("Shooted Gun")
		
		FireEvent:FireServer(Origin, Dir, BulletsCount, Spread)
		
		GunShotAnim:Play()
		
		Canrun = false
		
		task.wait(Cooldown)
		
		Canrun = true
	end
end)

serverscript :

local RPS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

local FireEvent = RPS.Events.RemoteEvents.FireGun

FireEvent.OnServerEvent:Connect(function (plr, Origin, Dir, BulletsCount, Spread)
	local RayParam = RaycastParams.new()
	RayParam.FilterType = Enum.RaycastFilterType.Exclude
	RayParam.FilterDescendantsInstances = {plr.Character}
	
	for i = 1, BulletsCount do
		local Direction = CFrame.new(Origin, Dir) * CFrame.Angles(math.rad(math.random(-Spread, Spread)), math.rad(math.random(-Spread, Spread)), math.rad(math.random(-Spread, Spread)))
		
		local RaycastResult = workspace:Raycast(Origin, Direction.LookVector * 150, RayParam)
		
		if RaycastResult then
			print(RaycastResult.Instance)
			local bulletpoint = Instance.new("Part")
			bulletpoint.Size = Vector3.new(0.3, 0.3, 0.3)
			bulletpoint.Material = Enum.Material.Neon
			bulletpoint.Color = Color3.new(255,0,0)
			bulletpoint.Position = RaycastResult.Position
			bulletpoint.Anchored = true
			bulletpoint.CanCollide = false
			bulletpoint.Parent = workspace
			Debris:AddItem(bulletpoint, 5)
		end
	end
end)
2 Likes

I heavily recommend raycasting on the client and sending the raycasts information to the server instead of raycasting on the server.

Although I can’t see why it would be offset, I do not recognize some of the methods that I haven’t used when making a raycast gun system.

thanks for reply, i’ll try that tomorrow.

Ok, so using the Mouse position instead of the LookVector seem to works

local Dir = Mouse.Hit.Position

i also put the raycasting on client like @PorchThatHuantsMe told.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.