Opening a door on the opposite of player's position

Hi, I’m making and testing a keybind door that opens in one direction, not the direction the player is facing, but a set one. I want to make the door open in the opposite direction of the player. I was wondering if there is any certain method, I could implement in my code, to solve my problem. I’ve heard the use of Dot() is useful, but I don’t know how I would implement it here. Sorry for the sloppy code, one of my first projects!

Video of the code, down below

https://i.gyazo.com/a0b3792b2385913ea9703c9029bdbc3b.mp4

local function OnInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.E then
for _,d in pairs(doors:GetChildren()) do
local goal = {}
goal.CFrame = d.Pivot.CFrame * CFrame.Angles(0, math.rad(degree), 0)
local tinfo = TweenInfo.new(0.5)
local t = ts:Create(d.Pivot, tinfo, goal)
if (game.Players.LocalPlayer.Character.PrimaryPart.Position - d.Pivot.Position).Magnitude <= 6 then
if can == false then return end
if can == true then
can = false
if open == false then
degree = 90
open = true
t:Play()
t.Completed:Wait()
else
degree = -90
open = false
t:Play()
t.Completed:Wait(1)
end
end
can = true
end
end
end
end

uis.InputBegan:Connect(OnInputBegan)

Video of the code, down below

1 Like

There’s probably a much better or more scientific way to do this but in the past what I’ve done is a quick distance check between the player and the door + lookvector comparted to the player and the door - lookvector. That will always be an accurate way of determining what side they are on.

I got the magnitude bit, but LookVector? How would I determine that.

local PlayerToDoor = (Door.CFrame.LookVector - Character.PrimaryPart.LookVector)

Like this? Sorry for my slow comprehension. But could you elaborate? Thank you.

(hrp.Position - (door.Position + door.CFrame.LookVector)).Magnitude
(hrp.Position - (door.Position - door.CFrame.LookVector)).Magnitude

Where hrp is HumanoidRootPart! Which ever of these two numbers are smaller is the side of the door they are on. LookVector is essentially just the direction the part is facing. You can check which way is forward by adding a surface to the part and then checking which of the directions is Front.

2 Likes

Thank you, I’ll try that right now.

1 Like

If you wanted to use Dot Products you would always be comparing the LookVector of the door to a Unit Vector of the player’s position to the door. Dot Products return a value between 1 to -1. 1 Meaning the two Unit Vectors are facing in the exact same direction and -1 meaning they are facing in the complete opposite direction.

This is useful since you can identify which side the player is on based on the sign of the Dot Product. This is because the LookVector of the door will always be facing outwards on one side of the door, but the player’s UnitVector to the door will always be facing at the door. So you will have situations where the Dot Product will be closer to 1 and situations where it’s closer to -1.

And once you know the sign of the Dot Product you now know which side of the door the player is on.

(I’ll work on some code to clarify this a bit more)

4 Likes

You would probably want to use a unit vector between the player and the door in case they are backwards but that’s definitely a good way to do it too.

Oh true, I never really considered that some people might want to approach the door backwards. Thanks for mentioning this.

1 Like

I think your method is better to be honest, the one I mentioned might actually be the hacky way to do it lol. Not sure it amounts to much in terms of performance either way.

Thank you guys for the help, is there a way I can make two solutions?

No, you can only pick one solution for the question.

2 Likes

Give it to @xZylter I like his solution more than mine anyway

2 Likes

Thank you for the help as well, this is my first post so your help means a lot to me

2 Likes