Using raycast, or any efficient method, how would I detect if a player is facing a character’s back?
basically, I have a blocking feature for my sword, but I want it so they can take damage if they get hit from the back. How would me, the attacker, determine if I’m attacking the back of the player?
(this method is obviously exploitable if security system/sanity check doesn’t exist)
Make a player spawn with a part in their back with welds.
And when the sword hit the backpart, they can take damage even when blocking is true.
local Difference = (Player2Position - Player1Position).Unit
local LookVector = Player1CFrame.LookVector
local DotProduct = Difference:Dot(LookVector)
if DotProduct < 1 then
--Player2 is facing completely opposite direction
end
local isFacingBack = false
local Difference = (character:FindFirstChild("HumanoidRootPart").Position - hit.Parent:FindFirstChild("HumanoidRootPart").Position).Unit
local LookVector = hit.Parent:FindFirstChild("HumanoidRootPart").LookVector
local DotProduct = Difference:Dot(LookVector)
if DotProduct < 1 then
isFacingBack = true
--Player2 is facing back of player 1
end
if isBlocking == true and userSwinging.Value == true then
local isFacingBack = false
print('function runs')
local attacker = character:FindFirstChild("HumanoidRootPart")
local target = hit.Parent:FindFirstChild("HumanoidRootPart")
local Difference = (attacker.Position - target.Position).Unit
local LookVector = target.LookVector
local DotProduct = Difference:Dot(LookVector)
print(DotProduct)
if DotProduct < 1 then
isFacingBack = true
--Player2 is facing completely opposite direction
end
if isFacingBack == true and debounceDamage == true then
DamagePlrBack(hit, character)
end
end
Set your dot product range depending on how accurate you need it to be.
If Dot Product is 1 of both player’s lookVector, that means they are facing in the exact same direction.
So you might set
local range = 0.75
if DotProduct > range then
isFacingBack = true
--Player2 is facing back of player 1
end
This is how I made my backstab mechanic, you can use it
local TargetPosition : Vector3, BackstabberPosition :Vector3 = Victim.HumanoidRootPart.Position, Backstabber.HumanoidRootPart.Position
local Direction : Vector3 = (BackstabberPosition - TargetPosition).Unit
local VictimFace : Vector3, BackstabberFace : Vector3 = Victim.HumanoidRootPart.CFrame.LookVector.Unit, Backstabber.HumanoidRootPart.CFrame.LookVector.Unit
local Result = Direction:Dot(VictimFace)
if Result < -0.5 then
print("Backstab") -- You are behind the target
else
print("Not backstab") -- In front of the target
end