Checking if a player is behind another player within a certain radius

Hello so my game has a feature where if your behind a player who is blocking you will be able to damage them through their block. This is my current code to check if a player is behind another player.

									if blockablebehind == false then
										local lookVector = v.Parent.HumanoidRootPart.CFrame.LookVector
										if (lookVector:Dot(plr.Character.HumanoidRootPart.Position - v.Parent.HumanoidRootPart.Position) < 0) then

											parry = false
											blockable = false
											stunHandler(plr,v,stun_time,dist)
										end
									end

however the result comes out like this.
image
picture the blue line a player and the red dot is the location they are facing. The green line then cuts them in-half. Anyone behind the green line is considered behind the player. This is a problem because it still counts if they are to the side of the player or only very slightly behind a player.
How can I solve this issue? All help is greatly appreciated! Thanks in advance!

1 Like

Let’s call the person behind the player the enemy, for purposes of this solution.

The way I did it:

  1. Raycast from the enemy HRP to the player HRP. Get the normal vector from that raycast. (will point perpendicular to the face of the HRP of the face that was struck)
  2. Get the angle between the player’s CFrame and the normal generated in 1.
  3. Transform the 3 dimensional angle to 2 dimensions.
  4. The resulting angle tells you which side of the HRP is in between the player than the enemy. Pick the angle range you are testing for.
1 Like
local character1: Model -- player character being attacked
local playerCharacter: Model -- attacking players character

local displacementVector: Vector3 = character1.PrimaryPart.Position - playerCharacter.PrimaryPart.Position
local direction: Vector3 = displacementVector.Unit
local distance: number = displacementVector.Magnitude -- use this for your distance check

local character1LV: Vector3 = character1.PrimaryPart.CFrame.LookVector

local angle: number = math.acos(character1LV:Dot(direction))
if angle >= math.rad(90) then
    print("player is behind character 1")
end

I think this should work. It is getting the angle between the direction character 1 is facing and the displacement vector between the two characters.
This is just loose code on how to do it and you will have to fill in some variables.

8 Likes

Thanks for the help I will try this ASAP. I’ll mark as solution if it ends up working.

Works perfect! Just had to change angle >= to angle <= since it worked in reverse!
Thank you so much for the help!

Also would there be a way to make sure the attacking player is facing the same direction as the victim?

Yes, you could shoot a ray from the player, and if the ray ends up returning an object u just need to validate to see if there is a humanoid in the object (find other things if need be) and u should have that working.

Another small solution could involve magnitue, so you could use the repeat until loop to see if there are any targets within your desired distance and checking if they contain a humanoid etc, if they do (this your until part) then you shoot a ray to see if the person is infront of you. Sadly i cant provide you with code for this, but it should be simple to write

Edit: i didnt realise how late this response is :sweat_smile:

Hope this helps