I am trying to make an fps game, and I am using raycasting for the bullets, as from what I heard, it is a good method of hit registry.
However, while the raycasts do work and damages a player if both ends are standing still, as soon as any person moves, the raycasts become inaccurate and don’t hit, even if the person is being clicked on at point blank.
The start of the shot is in startercharacterscripts, while the script that calculates the raycast is on serverscriptservice.
-- starting script
local raycastOrigin = script.Parent:WaitForChild("RaycastOrigin")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local rayFolder = game.Workspace.Rays
mouse.TargetFilter = rayFolder
local pistolShot = game.ReplicatedStorage:WaitForChild("PistolShot")
local shooting = false
local reload = game.ReplicatedStorage:WaitForChild("GlobalReloadCheck")
mouse.Button1Down:Connect(function()
if game.ReplicatedStorage.ShotsLeft.Value > 0 then
if reload.Value == false then
if shooting == false then
local mousePosition = mouse.Hit.Position
local originPosition = raycastOrigin.Position
pistolShot:FireServer(mousePosition, originPosition)
shooting = true
wait(.175)
shooting = false
end
end
end
end)
-- raycast script
local PistolEvent = game.ReplicatedStorage:WaitForChild("PistolShot")
local rayFolder = workspace.Rays
local debris = game:GetService("Debris")
local range = 5000
PistolEvent.OnServerEvent:Connect(function(player, mousePos, originPos)
local direction = (mousePos - originPos).Unit * range
local result = workspace:Raycast(originPos, direction)
if result then
local char = result.Instance.Parent
local hum = char:FindFirstChildWhichIsA("Humanoid")
if hum and hum ~= player.Character.Humanoid then
local Head = char["Head"]
local Torso = char["Torso"]
local Larm = char["Left Arm"]
local Rarm = char["Right Arm"]
local Lleg = char["Left Leg"]
local Rleg = char["Right Leg"]
hum:TakeDamage(15)
Head.BrickColor = BrickColor.new("Persimmon")
Torso.BrickColor = BrickColor.new("Persimmon")
Larm.BrickColor = BrickColor.new("Persimmon")
Rarm.BrickColor = BrickColor.new("Persimmon")
Lleg.BrickColor = BrickColor.new("Persimmon")
Rleg.BrickColor = BrickColor.new("Persimmon")
wait(.125)
Head.BrickColor = BrickColor.new("Medium stone grey")
Torso.BrickColor = BrickColor.new("Medium stone grey")
Larm.BrickColor = BrickColor.new("Medium stone grey")
Rarm.BrickColor = BrickColor.new("Medium stone grey")
Lleg.BrickColor = BrickColor.new("Medium stone grey")
Rleg.BrickColor = BrickColor.new("Medium stone grey")
end
end
end)
Thanks
4/2/22 - It turns out that it’s only when the target moves that the raycast becomes innacurate, if the player shooting moves, it’s fine.