How would I make it that the back of the torso is more vulnerable to attacks?

Hey everyone,

Battlefront II has this amazing feature where the back of the player is their most vulnerable part, that is, if you get slashed from the back then you lose more HP than from the front. This prioritises defending your back, and its a feature I want to include in my game.
My idea was to split the hitbox into 2, having the front and back both part of the torso but ultimately 2 different hitboxes. But this doesn’t seem like the most efficient way to create this feature.
Anyone got ideas?

1 Like

Two options:
Use magnitude to determine whether the attack is coming from the front or from the back. If the player is being attacked from the back, you can increase the damage given to that player. This would require some math, which unfortunately I don’t know how to do. If you do go with this option, this is a good resource:

OR you could

Raycast a few rays in the angle that you want behind the player’s Humanoid Root Part, then detect if the attacking player is attacking from behind or not. This is much easier to implement, but in my opinion not as great as option 1.

1 Like

You could use dot product if you want to rely on the orientation of the player’s torso. If you know the direction that the target and attacker is facing, you can calculate how far “away” the target’s back is relative to the attacker.

If you know how dot product works, you can make three important observations:

  • The dot product of an unit vector to itself is 1.
  • The dot product of an unit vector to itself rotated 90 degrees is 0.
  • The dot product of an unit vector to its negative counterpart (rotated 180 degrees) is -1.

The only time that the attacker is facing the back of the target is if they’re facing in the same direction. So, what we should be looking for is if the dot product of their LookVectors is close to 1.

local targetTorso = target.Torso
local attackerTorso = attacker.Torso

local targetDirection = targetTorso.CFrame.LookVector
local attackerDirection = attackerTorso.CFrame.LookVector

--dot product returns a value between -1 and 1.
--a value of 1 would be if they're facing perfectly parallel, which is practically impossible.
--so, we should use a threshold that checks if they're ALMOST parallel instead.
local attackingBack: boolean = targetDirection:Dot(attackerDirection) > 0.9

image

3 Likes

dot product is the way to go like @Prototrode suggested, but if the attack hitbox is large, you may consider the alternative to use their positions to determine the attackerDirection

local targetTorso = target.Torso
local attackerTorso = attacker.Torso

local targetDirection = targetTorso.CFrame.LookVector
local attackerDirection = (attackerTorso.CFrame.Position - targetTorso.CFrame.Position).Unit