How would I check if a player is facing in the same or opposite direction relative to a part?

I’m not so good with CFrames, but I am working on a rail grinding system where players can hop on top of a part and grind up or down it. However, I’m not quite sure how I could achieve a detection method which returns whether if a player should go up or down a rail.

Below is an image to illustrate what I mean:

So the Dummy on the left would go up the screen when it hits the railing and the Dummy on the right would go the opposite direction. Another problem I have is that I don’t want players to grind on the rail if they hit it sideways rather than parallel, so they need to at least (for example) be 90 degrees facing that specific direction rather than having an whole 180 degrees detection. I’m completely stumped on this, so any guidance would be appreciated!

1 Like

You could use the Vector3:Dot method for this.

Example usage:

local function dotProduct(character, part)
	return character.PrimaryPart.CFrame.LookVector:Dot(part.CFrame.LookVector)
end

local product = dotProduct(workspace.TestCharacter, workspace.TestPart)

if (product > 0.75) then
	print("move forward")
elseif (product < -0.75) then
	print("move backwards")
else
	print("don't move")
end
6 Likes

works like a dream! thanks so much!

2 Likes