Hitboxes that are specifically created in front of the player

Hello.
I’ve seen a lot of times people use hitboxes like the one shown in the picture below for punching in their game.

I tried watching tutorial videos for this on YouTube, but they all had plain code that simply checks if your hand is touching another player. But I don’t want that. I’ve seen in some video tutorials that they use hitboxes that spawn directly in front of you, but I’ve never figured out how to make a hitbox spawn in front of you that’s always a specific distance away from you no matter where you’re looking. I already made the code that you punch players with, but I haven’t figured out how to make such hitboxes.

2 Likes

You must use CFrames and WeldConstraints. Made this code real quick:

local hitbox = -- your bo

game.Players.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function(character)
      local newHitbox = hitbox:Clone()
      newHitbox.Parent = -- parent it to whatever you want
      newHitbox.CFrame = character.HumanoidRootPart.CFrame + CFrame.new(5,0,0)
      local w = Instance.new("WeldConstraint")
      w.Parent = -- parent the constrait to whatever you want
      w.Part0 = character.HumanoidRootPart
      w.Part1 = newHitbox
   end)
end)

Make sure to check out these articles:

2 Likes

To get a CFrame that is right in front of the player, you can do

local Distance = 3
local HitboxCFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -Distance)

This will make the HitboxCFrame a CFrame that is 3 studs in front of you, it can also be done with CFrame.LookVector like this:

local Distance = 3
local RootCFrame = HumanoidRootPart.CFrame
local HitboxCFrame = RootCFrame + RootCFrame.LookVector * Distance

To cast a hitbox & get all parts in front of you, my approach would be:

-- // Define OverlapParameters
local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterDescendantsInstances = {Character} -- Ignores the character's parts

-- // Get the Hitbox CFrame & get the hit parts
local HitboxCFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -3)
local HitParts = workspace:GetPartBoundsInBox(HitboxCFrame, Vector3.new(4, 5, 2), OverlapParameters) -- This returns a table of parts

-- // Get characters hit
local HitCharacters = {}
for _, HitPart: BasePart in HitParts do
	local CharacterHit = HitPart:FindFirstAncestorOfClass("Model")

	-- // Check if there's no Model, it isn't a character or it's already in the table
	if not CharacterHit or not CharacterHit:FindFirstChildOfClass("Humanoid")
		or table.find(HitCharacters, CharacterHit)
	then continue end

	table.insert(HitCharacters, CharacterHit)
end
5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.