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.
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.
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
--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()
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: