What i’m currently making
I’m currently working on a door system where the door opens away from the player.
How i’m currently doing it
Currently im detecting it with 2 parts (One on each side of the door) and going thru all the touching parts with the :GetTouching() function. It works but i’m wondering if there is any better ways to detect what side of the door the player is standing.
This i have tried
I have tried using Egomoose’s Rotated region3 but i could not get it to work so now im using this…
I have also tried using a CFrame wich is looking at the player from the middle of the door. This did work when the door was not rotated but worked very badly when the door were rotated.
When the player touches a detector, get the position of the player relative to the door frame (not the door itself since it moves!). The door frame can be any fixed part that has the same orientation as the door.
Try this: whenever a character touches a detector, run the following code:
detector.Touched:Connect(function(other)
if <other is part of a player character> then -- enter your character detection code here
local humanoidRootPart = character.HumanoidRootPart
local charPos = humanoidRootPart.Position
-- this is the position of the character relative to the door
-- depending on how the door frame is oriented, either
-- x, y, or z will always be negative on one side and positive on the other side
local relPos = doorFrame.CFrame:pointToObjectSpace(charPos)
print(relPos)
end
end)
Edit (Didn’t read post completely)
What you’re doing with the parts seems proper already.
FrontPart.Touched:Connect(function(Object)
if Object.Parent:FindFirstChild("Humanoid") then
--open door away from front side
end
end)
BackPart.Touched:Connect(function(Object)
if Object.Parent:FindFirstChild("Humanoid") then
--open door away from back side
end
end)
Edit:
To make sure that there is still a player in the part or vise versa, you could use Part:GetTouchingParts() like what you’ve stated
FrontPart.Touched:Connect(function(Object)
if Object.Parent:FindFirstChild("Humanoid") then
if #FrontPart:GetTouchingParts() > 1 then
--open door away from front side
end
end
end)
If you want you can construct a Ray from the colliding part’s position to the detector’s position and use the third returned value from workspace:FindPartOnRay in conjunction with the detector’s CFrame’s Up, Right, and Look vectors. By comparing with the up right and look vectors (as well as their negative counterparts) you can figure out which side was touched.