Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I tell if a player is behind another player?

Asked by
Dominical 215 Moderation Voter
9 years ago

Like to check if someone was in front of, or behind another person. Would it be something like this?

function isBehind(plr1,plr2)
if plr1.Torso<plr2.Torso.CFrame.lookVector then
return true
end
end
0
You would probably have to do raycasting. Perci1 4988 — 9y
0
I am terrible at RayCasting. Dominical 215 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

There's more or less two ways to do this, and they both actually amount to the same thing.

What you have is sort of what it will look like, except that you wrote is just made up and not actually based on any of the math.


Math approach:

To determine to what extent a point is in a given direction, we use the Dot Product.

The direction we want is one part's lookVector; the direction from one player to the other is the difference of their Positions:

-- using A and B as parts (the player torsos):
-- Check if B is in front of A

forward = A.CFrame.lookVector
displacement = B.Position - A.Position -- (direction)

amountForward = forward:Dot(displacement)

if amountForward > 0 then
    -- Forward
else
    -- Behind
end

Or, we can use the CFrame and its Object Space.

Object space refers to using some object as the origin (so (0,5,0) is 5 studs "up" where "up" is the top of a brick; (0,0,0) would be the position of the brick rather than the origin)

whereB = A.CFrame:pointToObjectSpace( B.Position )
if whereB.z > 0 then
    -- Behind
else 
    -- In front
end
0
Are you a Wiki writer? Because you're surely good at ROBLOX's API and are a GREAT teacher! Thanks! Dominical 215 — 9y
0
I am a Wiki writer but I almost never write on the Wiki. BlueTaslem 18071 — 9y
0
You should, because you're an awesome teacher. No one on here explains things as good as you did. Good job. Dominical 215 — 9y
Ad

Answer this question