Custom hitbox for NPCs?

Greetings!
While working on my game, I’ve come across a problem: Enemies in my game bunch up too easily at tight spots, and for massive lag as well as completely jam their advance. Normally in any Tower Defense game enemies can’t collide with one another, but my game requires that as a meaningful feature. How would I go about making a custom (and smaller) hitbox for my enemies?
Thanks

Could you have the enemies themselves be CanCollide false and attach a part to their torso which is invisible but CanCollide true? You’d be able to completely control their hitbox size since the hitbox would be the part attached to the torso.

2 Likes

Instead of using the actual enemies body for a hitbox create a Part and turn off CanCollide and Anchored and add a WeldConstraint between the Torso and this new Hitbox Part.

2 Likes

I thought about this, would turning every collision off and making the humanoidRootPart collide and smaller work?

For this I would create a part and use a moter6d or weld it to the HumanoidRootPart. Change the sizing of the part for it to fit your needs.

local createCharacterHitbox = function(char)
    local torso = char:WaitForChild("HumanoidRootPart")
    local newPart = Instance.new("Part", char)
    newPart.Size = Vector3.new(2, 1, 3) -- Change this for your NPCs.
	newPart.Massless = true
	newPart.CanCollide = false
	newPart.Anchored = false
	newPart.Name = 'Hitbox'
	newPart.Transparency = 1

	local weld = Instance.new("Weld", torso)
	weld.C0 = CFrame.new(0, -2.5, 0)
	weld.C1 = CFrame.new()
	weld.Part0 = torso
	weld.Part1 = newPart
end

I hope this helps!

If I recall a normal Weld wont work because its meant to be a “strong” weld, last time I tried welding something to a character it just anchored them even though both parts were un-anchored. I think using a WeldConstraint would be more efficient.

1 Like

No, I won’t. I used this in my game :slight_smile:

you could use something like this

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if player then
					--hitmarker for players
				end
				if not player then
					--hitmarker for npcs
				end

Hmmm… But what does that do?

It gets the character from a model and if it finds a player then it will hitmarker for players, if it doesn’t find someone then it does hitmarker for npcs. I don’t know how your gun works so I don’t know if it will help you.

here is a link to read about it: Players | Roblox Creator Documentation

Well it’s not a gun, it’s collision. I’m trying to make my enemies take up less space so they don’t constantly cause traffic jams

Ohhhh, I read that wrong. I thought you were talking about hitmarkers.

When you spawn an NPC just use this script I use it in my games. You can resize the box if you want.

local createCharacterHitbox = function(char)
    local torso = char:WaitForChild("HumanoidRootPart")
    local newPart = Instance.new("Part", char)
    newPart.Size = Vector3.new(2, 1, 3) -- Change this for your NPCs.
	newPart.Massless = true
	newPart.CanCollide = false
	newPart.Anchored = false
	newPart.Name = 'Hitbox'
	newPart.Transparency = 1

	local weld = Instance.new("Weld", torso)
	weld.C0 = CFrame.new(0, -2.5, 0)
	weld.C1 = CFrame.new()
	weld.Part0 = torso
	weld.Part1 = newPart
end

Thanks. However, I’d much perfer to be able to customize it. Is it possible to make the humanoidRootPart do that?

No, not that I know of. You would have to create a part to act as the hitbox.

Alright! Thanks for the response.
I’m also assuming I have to make everything else in the humanoid uncollidable right?

Yes, but make sure the hitbox is as low to the ground as the feet or the NPC will sink into the ground :stuck_out_tongue:

For that I can just assign them into the same collision group.

Thanks for your help!

1 Like

No problem! I’m glad I could help!