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)