How to detect a Character's Back?

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?

2 Likes

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

1 Like

You could use Vector3:Dot

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
3 Likes

In your example, is player 1 or 2 doing the attacking

Player2 is meant to be the attacker in this case.

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

Would this be how you do it

P.S. this doesn’t seem to work

Yeah. If it doesn’t work, try printing DotProduct and change 1 accordingly.

That’s odd, it doesn’t print anything for some reason and the function does run

Can you send the part of the code?

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

It does print function runs

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
2 Likes

I did that, but for some reason, the printing for the dot product never prints, which is why I assume, the isFacingBack value never becomes true

Did a debugging test, basically, it doesnt run anything after
local LookVector = target.LookVector

Ohh forgot to do CFrame.LookVector

now it prints

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
3 Likes