I know this question has been asked before, but it wasn’t marked as a solution, and even after reading all of the replies I’m still stuck on this.
So I have 3 parts, 2 of them being an open door, the other one being a closed one. These 3 parts are all invisible and cancollide false, they are just there so the actual door can tween it’s CFrame to them.
How would I make the door open the way the player is facing? (Becasue it will be very annoying when it bumps into the player)
You can use dot product to get the side a player is on of the door.
The dot product is positive if they’re in front and negative if they’re behind
For example:
local function isPositionInFront(position)
return (door.Position - position).Unit:Dot(door.CFrame.LookVector) > 0
end
The red vector is the LookVector of the door, basically the vector that determines which way is forward. The purple vector is the vector constructed by subtracting the door position from the target position, which creates a new vector pointing at the target position. Both vectors must be normalized, and from there you can get the dot product. You can also get the actual angle in radians by getting the arcsine of the dot product.
Edit: Another way
local function isPositionInFront(position)
return door.CFrame:PointToObjectSpace(position).Z > 0
end
Basically gets the relative offset from the door cframe and checks if the Z position is positive