How would you detect which side of the door (or part) the player is on?

Hello! I’m making an interaction system that includes doors, as shown below.

The problem is that I don’t know how to calculate which side the player is on to ensure I open the door in the opposite direction so that the door doesn’t always hit the player opening the door.

I was told I could use :Dot(), but until now I’ve never heard of it and when I looked it up it seemed very confusing.

I have however managed to do a VERY hacky approach to temporarily solve my problem, but I’d like to improve it and ensure that it’s a stable approach rather than what I have now.

Video of current code:
https://streamable.com/xhilre (In link form because it wouldn’t upload)

Current code that calculates which side the player is on:

local relativeCFrame = character:WaitForChild("HumanoidRootPart").CFrame * StaticDoorPart.CFrame.lookVector

if relativeCFrame.Z > -47 then
	tweenModel(door, door.Parent:WaitForChild("BackOpenPosition").CFrame)
else
	tweenModel(door, door.Parent:WaitForChild("FrontOpenPosition").CFrame)
end

I know this might be a very ineffective solution I’ve got but it’s all I could figure out.

9 Likes

You can use two different parts, so when one get touched you can rotate the part in the right way

I could, but I would rather not to simplify things for the builders of the game.

I’ll say you can use RayCasting, but using it you must be sure the player is in front of the door. Else you can use a Region3, but this could make the things a bit more complicated

Use this simple code to detect which part is near then insert your code for the position you want the door open at.

local player = game.Players.LocalPlayer
local character = player.Character
local door = workspace.Door
local back = workspace.Back
local front = workspace.Front
while wait() do
local back = (back.Position - script.Parent.Torso.Position).Magnitude
local front = (front.Position - script.Parent.Torso.Position).Magnitude
if back > front then
	print("Front " .. math.floor(front))
	else
	print("Back " .. math.floor(back))
end
end

Edit that code on your style.

:Dot() is just a simple means of saying “How similarly do two Vector3’s face?”
If they are facing largely in the same direction, :Dot() returns a positive number.
If they are perpendicular, :Dot() returns 0.
If they are facing in opposite directions, :Dot() returns a negative number.

Consider the following segment:

local doorToChar = character.HumanoidRootPart.Position - StaticDoorPart.Position 
--Get a vector that goes from the door to the character
local doorLookVect = StaticDoorPart.CFrame.lookVector
--Get the vector where the door itself faces
if doorToChar:Dot(doorLookVect) > 0 then
--Character is in front of the door
else
--Character is behind the door
end

23 Likes