Hello,
I am trying to make a basic raycast from in front of the player’s HRP to 5 studs ahead, and this works fine standing still. But when the player is moving, the raycast seems to lag behind and it doesn’t seem to really begin the cast in front of the HRP like I want it too.
Here is the script (serverscript):
sendAttack.OnServerEvent:Connect(function(player,attackCooldown)
if attackDebounce == false then attackDebounce = true
coroutine.wrap(function()
task.wait(attackTime)
castRay()
end)()
task.wait(attackCooldown)
attackDebounce = false
end
end)
function castRay()
local origin = rootpart.Position
local direction = rootpart.CFrame.lookVector * 5
local rayParams = RaycastParams.new()
local rayResult = workspace:Raycast(origin,direction,rayParams)
if rayResult then
if rayResult.Instance.Parent:FindFirstChild("Humanoid") then
rayResult.Instance.Parent.Humanoid:TakeDamage(damage)
print("Did "..damage.." to "..rayResult.Instance.Parent.Name)
end
end
end
Is there a good way to correct this behavior? And while I’m asking questions, is there a good way to fire the same ray three times, except it fires from the HRP slightly up or down (preferably a way to control it from where the function is fired, which is the attack function)?
Any help is appreciated!