How can I improve this raycast combat system?

Hi DevForum!

I made a RayCasting combat system instead of using a hitbox, and wanted to know if I can improve some aspects, here’s my script:

--// Services
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

--// Data
local AnimationsFolder = RS:WaitForChild("Animations")
local Animation = AnimationsFolder.Punch

--// Variables
local player = game:GetService("Players").LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local enemyCharacter = nil

local combo = 1
local lastM1 = 0
local lastEnd = 0


local KeyBinds = {
	["M1"] = Enum.UserInputType.MouseButton1,
	["M2"] = Enum.UserInputType.MouseButton2
}


local PunchAnims = {
	"rbxassetid://9671655612",
	"rbxassetid://9671675001",
	"rbxassetid://9671655612"
}


local function raycastTarget()
	-- 4 studs
	
	local hrp = character.HumanoidRootPart
	
	
	
	local rayMagnitude = 4 -- Distance in Studs
	
	local rayOrigin = hrp.Position
	local rayDirection = rayOrigin + hrp.CFrame.LookVector * rayMagnitude
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	if raycastResult then
		
		if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			print("Found a humanoid!")
			
			warn(raycastResult.Instance)
			warn(raycastResult.Instance.Parent)
			
			enemyCharacter = raycastResult.Instance.Parent
			
			-- DO WHATEVER ELSE HERE!! (for alee)
		end

	end
end


UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	
	if input.UserInputType == KeyBinds.M1 and tick() - lastM1 > 0.3 and tick() - lastEnd > 0.75 then
		
		if tick() - lastM1 > 0.75 then
			combo = 1
			print("Combo Reset!")
		end
		
		
		lastM1 = tick()
		
		Animation.AnimationId = PunchAnims[combo]
		
		local loadAnim = humanoid:LoadAnimation(Animation)
		loadAnim:Play()
		
		
		raycastTarget()
		
		
		
		if combo >= 3 then
			combo = 1
			lastEnd = tick()
			
			print("Combo ended!")
		else
			print("Combo", combo)
			combo += 1
		end
		
	end
end)

Thanks for the advice once you comment! Also another question, should I use hitboxes or raycasting for combat systems?

Thanks again!

4 Likes

Ok, first off I want to complement you on how readable this script is.

Now to get down to business, you need to consider how difficult it could be for your players to actually hit someone, currently the hit detection you have is run by a singular ray (which if I have time I can find the module that does the heavy lifting). Now, all preferences by nature are subjective, it’s solely dependent upon what you desire your system to become or what you feel is right.

Through raycasting, the hitbox sort of wraps to the animation, now this comes with its benefits, and possibly disadvantages. For one, you may need to be mindful of when making an animation, asking the question “is there going to be difficulty in hitting another player given the current movement?”. There is also a great advantage to using a raycast hitbox: through a module I used a while back I found that I was able to find the point of (nearly) direct intersection with the weapon model (or in this case whatever you’re planning on using) and the environment. This could be useful for making some sort of reaction to the surrounding environment, such as in a popular game “Combat Warriors”, where collisions are portrayed with smoke when a weapon hits an in-game object.

Now let’s get down to a hitbox. As far as complexity, these do not require a lot of time and can be pulled off like a parlor trick. They do not wrap to animations and so you do not have to be mindful while animating at all, as they will be consistent. Another semi-popular game, “Lightsaber Battlegrounds”, uses a magnitude hitbox, which in practice is probably one of the most simple hitboxes. A downside I can see is that they do not respect any aspect of the environment the player is around in and so you cannot get intersection points which you could use to create some sort of collision. Another downside is that these types of detections engulf a large area, an area possibly greater than the weapon or fist swept in.

Aside from the pros and cons of both, I can not speak to the efficiency of either as I have never researched such a thing. However, given hundreds of rays being able to be casted for a raycast-based detection system, and the possibly simple calculation by hitbox-based detection systems, I cannot see how either can be that computationally intensive otherwise it would have set off red flags throughout the community to cease their use.

(Keep note that this has been the first time I have ever attempted to answer a question, if anything is unclear tell me. I do not normally do this in any form.)

3 Likes

Raycast Hitbox 4.01: For all your melee needs! (If you want to look into a raycast-based hit detection system)

3 Likes