How to see if the player is facing the back or the front of their character

Sorry if the title sounds a bit obscure, it’s hard to describe this. Basically I’m trying to make it so if your camera, relative to the player’s HumanoidRootPart is past 90 degrees. I’ve managed to get it working some what, but when the character is oriented at certain angles, being 5 degrees away from the HumanoidRootPart’s angle returns numbers from around 340 to 360 instead.

The use case is for tilting the player’s head at the X angle for looking up and down. When they are past 90 degrees (looking at their character’s face) I want it to face towards the camera instead of towards the ground. (and when they are facing the back of their character, they face towards where the camera is looking)

ReferencePart.CFrame = Camera.CFrame
local HeadAngle
local CameraHeadOffset = math.abs(ReferencePart.Orientation.Y-Character.HumanoidRootPart.Orientation.Y)
if CameraHeadOffset < 90 then
	HeadAngle = math.rad(ReferencePart.Orientation.X)
else
	HeadAngle = -math.rad(ReferencePart.Orientation.X)
end
1 Like

Just a thought, but have you looked into perhaps using the Dot Product of the Camera’s lookVector and the character’s torso’s lookVector? (You can nullify the Y components of the vectors to just get the vectors on the X/Z plane, then use the dot product to see whether the camera is facing the face of the character or the back of it)

I’ll give that a shot, thanks for the idea.

So I’ve tried a few things but it seems to only depend on Camera.Orientation and turning the character did nothing. I might try doing magnitude and see how that goes.

local CameraHeadOffset = Vector3.new(
	Character.HumanoidRootPart.CFrame.lookVector.X,
	0,
	Character.HumanoidRootPart.CFrame.lookVector.Z
):Dot(
	Vector3.new(
		Camera.CFrame.lookVector.X,
		0,
		Camera.CFrame.lookVector.Z
	)
)

MAYBE, the problem isn’t the script? Make sure that the front of the part (the part that holds the players camera) is facing forward?

The part is just for getting an orientation from the CFrame because CFrame:ToOrientation wasn’t working for me. The part’s orientation is set every frame.

1 Like

I think you forgot to normalize the vectors; try this:

local dot = Vector3.new(
    Camera.CFrame.LookVector.X, 
    0, 
    Camera.CFrame.LookVector.Z
).Unit:Dot(Vector3.new(
    Character.Head.CFrame.LookVector.X, 
    0, 
    Character.Head.CFrame.LookVector.Z).Unit
)

After running a quick test in studio, it seems to work (I simply printed dot every frame. If the result is positive, the camera is behind the head, otherwise it is infront of the head). You can change Character.Head.CFrame to whichever part of the character you want to check from.

4 Likes

It’s perfect! Thank you so much :smiley: