"Gun accuracy" offset isn't working as intended

Hello!
I have been working on a project of mine, and I’ve ran into a problem.

While making a gun, I wanted to add offset to the ray. This is how I did it below:

-- origin, direction, and point are assigned in a local script then returned to server. direction is the mouse hit position, while point is a "test rays" position to calculate distance affectively
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {char, tool}
		
local dist = (origin - point).Magnitude
	
local offset = Vector3.new(
	math.random(-100, 100),
	math.random(-100, 100),
	math.random(-100, 100)
) / 1000 * dist
		
local raycast = workspace:Raycast(origin, direction + offset, params)
		
if raycast then
	createTrail(origin, raycast.Position + offset) 
	local humanoid = getHuman(raycast.Instance)
	if humanoid then
		num += 1 
		print(humanoid.Parent, num)
		if raycast.Instance.Name == "Head" then
			humanoid.Health -= module.HeadshotDamage
		else
			humanoid.Health -= module.Damage
		end 
	end
else
	createTrail(origin, direction + offset)  
end
return 

Here is what happens: (resolution may be a bit low)

As you can see in the video, (if it works), the bullet spread is going everywhere except…
it says the bullet has hit the NPC I was aiming towards all 10/10 times. Which is obviously not what happened.

Any help is appreciated.

1 Like

Mmm i dont get the code but maybe try setting the

createTrail(origin, raycast.Position + offset) 

to

createTrail(origin, raycast.Position) 
4 Likes

…Thats literally for the trail. I need help with the Raycast. NOT the trail

And while yes, when I remove it, the trail is straight. which means the accuracy is not working. Why is this? This is what I need help with.

2 Likes

Found the issue. I’ll mark this for anyone that find this.

Apparently, after sending mouse positions to server, you cant offset them. It will literally just return the exact mouse position no matter what offset you add.

Because of this, you have to add the offset in the CLIENT, then return it to server.

--Client
local origin = tool.BulletStart.Position
local direction = (player:GetMouse().Hit.Position - origin).Unit * 5000
local point
	
local testRay = workspace:Raycast(origin, direction)
if testRay then --Dont really know if firing two rays is practical to measure distance, but i will do it anyways.
	point = testRay.Position
else
	point = direction
end

local dist = (origin - point).Magnitude
local offset = Vector3.new(
	math.random(-100, 100),
	math.random(-100, 100),
	math.random(-100, 100)
) / 1000 * dist

local newDirection  = (player:GetMouse().Hit.Position+offset - origin).Unit * 5000
fireEvent:FireServer(origin, newDirection)

--Server
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {char, tool} 
		
local raycast = workspace:Raycast(origin, direction, params)
		
if raycast then
	createTrail(origin, raycast.Position) 
	local humanoid = getHuman(raycast.Instance)
	if humanoid then
		if raycast.Instance.Name == "Head" then
			humanoid.Health -= module.HeadshotDamage
		else
			humanoid.Health -= module.Damage
		end 
	end
else
	createTrail(origin, direction)  
end
return
1 Like

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