Detecting if player in line

Hey developers! I’m currently working on my game and I need a way to detect if a player is in line with a player beside him (to his/her left or right). What ways do you think I should go back doing this and what should I use.

Any help loved

1 Like

One way to do this would be raycasting towards both sides and checking if any players were hit.

1 Like

You can transform a CFrame or Position into one that is “relative” to another CFrame. You can then check this position without having to worry about any rotation or such, as if both players were near 0,0,0 and oriented the default way.

local cframeA = workspace.PartA.CFrame -- HumanoidRootPart of first player
local cframeB = workspace.PartB.CFrame -- HumanoidRootPart of other player

local function isBBesideA(cframeA, cframeB)
	-- local cframeRelative = cframeA:ToObjectSpace(cframeB)
	local posRelative = cframeA:PointToObjectSpace(cframeB.Position)
	
	return math.abs(posRelative.X) < 6
	and    math.abs(posRelative.Y) < 3 -- could probably be reduced
	and    math.abs(posRelative.Z) < 1.5
end

print("B is beside A?", isBBesideA(cframeA, cframeB))
print("A is beside B?", isBBesideA(cframeB, cframeA)) -- exactly one of these will be false when e.g. you have the characters forming a T shape

To be strict and only consider players to be beside each other, do isBBesideA(cframeA, cframeB) and isBBesideA(cframeB, cframeA)
To be less strict and allow that T case, do isBBesideA(cframeA, cframeB) or isBBesideA(cframeB, cframeA)
(the difference is the “and” vs the “or”)

Some bugs I know this will have:

  • It will probably consider players to be beside each other if they are in front of each other without any space between
  • It will still try its hardest when one of the players is lying flat on the ground, or otherwise not simply standing up straight
1 Like

Heyo Ninja.
You can DM me if u need any help on how to accomplish this.

What I would do.
Is create a raycast to the nearest player from the character and set a max distance away.
If the character is within the max distance then you use a for loop to get the next nearest while adding to a table to remove the players already considered in a line.

Thats how its done in EI and how I have used it myself for other projects.