Raycast for Sword hit detection

I am basically firing multiple rays along the blade to detect if it has hit something. The problem is I am only able to fire 8 rays in 0.3 secs (how long the animation lasts), which creates big spaces between rays where a part can be undetected.

I wanted to know if there’s a better way to detect a hit without using the touched event or a way to fire more rays.

edit: I am using the spawn function to run the detection

function DetectPart() -- Raycast to find Part
	while Slashes[Amount].IsPlaying do
		wait()

		local Start = Sword.Point.CFrame
		local NewRay = Ray.new(Start.Position, Start.lookVector * 4.5)
		local Part, Position = workspace:FindPartOnRayWithIgnoreList(NewRay, Character:GetDescendants())
		
		if Part and Part.Parent:FindFirstChild("Humanoid") then

			Sword.Blade.Hit:Play()
			Part.Parent.Humanoid:TakeDamage(5)

			break
		end
	end
end
1 Like

If you want to create more rays in a shorter amount of time you can use RunService and Heartbeat

local RunService = game:GetService("RunService")
RunService.Heartbeat:Wait() 
-- instead of wait()
6 Likes

Oh so then what’s the default wait() value, I thought it was small enough to allow for a lot of rays.
Thanks this worked by the way!

As of now, wait() will yield for whatever amount of time you input, but will not go any lower than 0.03, or about 1/30th of a second.

Should the need for shorter delays arise, I resort to RunService.Heartbeat:Wait() in Scripts (or RenderStepped:Wait() in LocalScripts). Unfortunately this is locked to 1/60th of a second (0.016 seconds)

Source is from this post

3 Likes