Why is my raycast inaccurate?

Hello.

I have been adding Raycast based hit scan for lasers in my game, starting from the player and going towards the player’s mouse. However, there is a problem. The ray cast is EXTREMELY inaccurate and ends up completely missing the target and either going way above or below the target position. If you can help me fix this, please do. Thank you. I will provide the code now.

This is a ServerScript.
cam is the Camera CFrame, hit is mouse.Hit.p

game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr,cam,hit)
	local Raycast = Ray.new(cam.p, (hit.p - cam.p).unit * 300)
	local hp, hpp = game.Workspace:FindPartOnRay(Raycast, plr.Character, false, true)
	print(0)
	if Raycast then
		print(1)
		if hp then
			print(2)
			print(hp,hp:GetFullName())
			local LaserBeam = Instance.new("Part", game.Workspace)
			LaserBeam.BrickColor = BrickColor.new("Bright green")
			LaserBeam.FormFactor = "Custom"
			LaserBeam.Material = "Neon"
			LaserBeam.Transparency = 0
			LaserBeam.Anchored = true
			LaserBeam.CanCollide = false

			local LaserDistance = (plr.Character.PrimaryPart.CFrame.p - hpp).Magnitude
			LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
			LaserBeam.CFrame = CFrame.new(plr.Character.PrimaryPart.CFrame.p, hpp) * CFrame.new(0, 0, -LaserDistance/2)
			if hp.Parent and hp.Parent:FindFirstChild("Humanoid")
				or hp.Parent.Parent and hp.Parent.Parent:FindFirstChild("Humanoid") 
				or hp.Parent.Parent.Parent and hp.Parent.Parent.Parent:FindFirstChild("Humanoid") then
				local i = hp.Parent:FindFirstChild("Humanoid") or hp.Parent.Parent:FindFirstChild("Humanoid") or hp.Parent.Parent.Parent:FindFirstChild("Humanoid")
				i:takeDamage(70)
			end
		end
	end
end)

Have you tried applying a manual offset to it? Perhaps rotating the origin of the raycast up or down in the opposite direction may be what you need.

I don’t think adding an offset would help because the inaccuracy increases the further from the target you are.

If you add a rotation offset at the origin of the raycast, would that not help the accuracy of the raycast?
don’t judge my art skills i made it in like 2 mins bruh
image
This explains why if you are further away, the inaccuracy is far greater.

How would I apply the rotation in the way you described?

I’d click play and manual rotate the origin of the raycast using the explorer until it is spot on, and measure the offset. Then, its just a matter of adding / subtracting a Vector3 value representing the orientation.
Let me know how it goes!

The origin of the raycast is the player’s PrimaryPart, and I can’t rotate that.

You could use an alternative, such as cloning the primary part, and making it invisible and setting its CFrame to the primary part. This means you can rotate it without problems hopefully. It explains why the raycast is inaccurate, since the primary part is always going to be moving around and stuff.

1 Like

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