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.
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!
Let’s call the person behind the player the enemy, for purposes of this solution.
The way I did it:
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)
Get the angle between the player’s CFrame and the normal generated in 1.
Transform the 3 dimensional angle to 2 dimensions.
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.
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.
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