Checking if the player is infront of another player

I’m making a combat system that uses magnitude and im trying to see if the player is infront of the other player that he’s hitting I searched about this topic and found some topics relating this and I did find the way to check the angle between the 2 players, but my problem here is that I cant seem to figure out the right numbers in order to see if the player is infront of him, I tried some numbers but the script printed “infront of the player” when i was indeed behind him and some spots just didnt count regardless of me being infront of him

Here’s the script:

local player = game.Players.LocalPlayer



game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
	if gpe then return end
	
	if input.KeyCode == Enum.KeyCode.F then
		local looking = player.Character.HumanoidRootPart.CFrame.lookVector

		local direction = (workspace.Dummy.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Unit


		local dot = looking:Dot(direction)
		local angle = math.acos(dot) 
		angle = math.deg(angle) 
		
		if math.floor(angle) <= 11 and math.floor(angle) >= 6 then
			print("infront")
		end
		
	end
end)

If anybody have done this before or knows the correct numbers that would be highly appreciated.

3 Likes

Hi, you can use workspace.CurrentCamera:WorldPositionToScreenPoint(target pos) this gives you to info, position on screen and boolean which mean like position on screen or no. Thats what you need

local relPos = player.Character.HumanoidRootPart.CFrame:PointToObjectSpace(workspace.Dummy.HumanoidRootPart.Position)

if (relPos.Z < 0) then
	print("dummy is in front of player")
else
	print("dummy is behind player")
end
1 Like

It prints dummy is infront of the player regardless of where I am.

I didnt quite understand what you mean, Can you give me an example code?

Sure
local ScreenPos, OnScreen = workspace.CurrentCamera:WorldPositionToScreenPoint(target.Position)
if OnScreen == true then
print(“Dummy on screen”)
end

Im not trying to check if the dummy is on screen, Im trying to check if my player is facing the dummy

Oh sorry, so you need get angle between vectors?

Yes thats what im looking for.


So by this formula you gets cos between vectors, for get angle you need put number which you get to this: math.deg(math.acos(your number))

In the post I clarified that I was able to get the angle between 2 players my problem is that the output number is not consistent and I cant be 100% sure if the player is really facing the player or not

Anyway try this formula, i using it for check rickoshet at my game this never gived me wrong number, so its must work

By number do you mean (workspace.Dummy.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position)?

No, at number i mean cos which we gets from this formula

I dont understand even a tad bit of that formula

You can determine this based on the dot product of the other player’s location.

local thisPlayer: (Player) = game.Players.LocalPlayer
local otherPlayer: (Player) = game.Players.Megumin

local function isPlayerInFrontOfPlayer(player1: (Player), player2: (Player)): bool
	local dirToOtherPlayer: (Vector3) = (player2.Character.PrimaryPart.Position - player1.Character.PrimaryPart.Position).unit
	return player1.Character.PrimaryPart.CFrame.LookVector:Dot(dirToOtherPlayer) > 0.45
end

print(isPlayerInFrontOfPlayer(thisPlayer, otherPlayer))

image

image

image

The dot product, in simple terms, provides a “percentage” (-1 to 1) of how close two vectors are to being parallel. 1 = parallel and 0 = perpendicular. -1 = going in opposite directions.

image

image

11 Likes

You know what is vectors and cos in geometry and trigonometry?

Is there any way to make the blue line to move in the same direction as the red line, for instance: 0.2’s red line = 0.2’s blue line