How would I check if a player is facing a part

How would I check if the player is facing the front or back of a part?

i’d raycast from the part in the front and back directions, and check if the rays were hit constantly. whatever hit it would be something (you could filter it and get a player)

thats actually a really complicated way to do it, but i know you can use lokvector to your advantage.

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

1 Like

What would be the best way for making a door open both ways and close both ways? I’m trying to detect if the player is facing back or front of a door. Kind of like fortnite

Then you would have to rely on the doors rotation.

That wouldn’t be really convenient because I want it to open depending on how its facing.

You probably don’t need to know if the player’s facing the front or back of the door in this case and can instead manipulate the direction of turning based on the door’s position relative to the character. Put the door in object space to the character and check the Z value of the given CFrame for a positive or negative value and adjust the rotation accordingly.

3 Likes

It is.

My idea is that I would raycast the front and the back of the door repeatedly by only raycasting a few studs, then if one of the raycast hits a player, then I will identify which direction the raycast would cast out and open the door or close it correspondingly.

Im using proximity prompt how would I do something like that?

Oh, that makes it so much easier.

When the proximity prompt is fired, you raycast from both the front and back of the door and follow whatever steps I’ve said previously.

1 Like

Do you have any sources I could use to learn how to use raycast? This is how I have the door opening.

for i = 1, 21 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-5), 0))
			wait()
		end
1 Like

How would I find the raycast direction of the player? All I have seen is vector3.new which is creating a new direction

I’m 90% sure the correct answer is: Dot Product.

That said, you should learn about ray casting because it’s really useful and you’ll have a better handle on all kinds of problems in the future.
The link notzeussz posted is great.

Ray casting several samples from a door to see if sees the player or raycasting samples from the player to the door is a fragile and expensive way to go about finding out if a player is “facing” a door.

Vector A: the vector facing forward from the player
Vector B: the vector from the player to the center of the door
In my mind, I’d say anything less than 20 degrees difference is the player facing the door, but maybe you take the distance to the door into account.
Dot product to find the difference:

image

Dot product between A and B tells you the player is looking in the general direction of the door. If you need to know which side of the door the player is on, you can still use dot product.

Vector C: vector from the door hinge to the opposite side of the door frame
Vector D: vector from the door hinge to the player
Dot product of C and D will be positive on one side of the door and negative on the other. I think.

Did I miss anything?

1 Like

Thank you very much, I’ve been learning ray casting and found out how to make my problem work.