Checking if another player is in or out of hitbox

Hello, I’m creating a hitbox that determines whether or not a player blocks an attack from the front, this hitbox is continously updated to always be in front of the player.

Code for hitbox:

I believe the for loop is the main issue, I think right now what determines the value of the bool is the last “thing” that was found in the hitbox.

Is there a better way to check if another player is in or out of the hitbox? Any help is appreciated thank you for your time.

2 Likes

This doesn’t seem to be a very effective way of doing things. You use a hitbox that determines whether a player is infront of the player, and only then is that when they’re blocking.

If you want to see whether an attack is from the back or the front, use positions and magnitude to find the difference. Whenever an attack lands, compare the distance of the attacker between a position slightly infront of the target versus slightly behind the the target. The shorter distance is closer, and thus would be an attack from that direction.

Do you mean like this?

I’m having trouble with the comparisons, how can I prevent both CFrames from becoming the same value?

1 Like

are u trying to prevent both magnitudes from becoming the same value?

2 Likes

Yeah, I’m trying to check if the other player is closer to the front or back of the local player basically. I want the smallest value but both values become the same

1 Like

so this is what i would do

--run the function every time a player calls a block

local function checkMagnitude()
	local minDistance = 0.1 --(you can change this to what u want. may work better with a larger number like 1.)
	local frontComparison = (attackerPosition - frontCFrame.Position).Magnitude
	local backComparison = (attackerPosition - frontCFrame.Position).Magnitude

	if math.abs(frontComparison - backComparison) <= minDistance then
		--distance is more less the same
		local offsetCFrame = frontCFrame * CFrame.new(0, 0, -0.5) --offsetting the frontCFrame 0.5 studs forward (you can adjust)
	elseif frontComparison < backComparison then
		--player is closer to the front of the local player
	else
		--player is closer to the back of the local player
	end
end
checkMagnitude()
2 Likes

Can you specify what “become the same” means? If you’re worried about what happens if for some reason the attacker is in the exact same position as the target, just use the =< (less than or equal too) operator in one of the statements.

Also, I would make the offset something like 0.1-0.5 studs, not 2.5.

My apologies, what I mean is that “frontComparison” in my code above is always less than “backComparison”, no matter if you attack someone blocking from the front or back. This is my updated code following target’s method:

Outputting the comparisons (attacking front and back of player):
image

I tried my previous method and the same occured

My problem has been solved, thank you both for your time! I was just getting my own variables mixed up.

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