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.
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
Sure
local ScreenPos, OnScreen = workspace.CurrentCamera:WorldPositionToScreenPoint(target.Position)
if OnScreen == true then
print(“Dummy on screen”)
end
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
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.