Why is this script not printing Left or Right?

Hey there! I’m trying to make a script that prints “Left” or “Right” depending on if the camera is looking left or right from the HumanoidRootPart while in first person and the HumanoidRootPart rotation is locked.

The script (Local):

if cam.CFrame.Rotation.Y < humanoid.RootPart.CFrame.Rotation.Y then
	print("Left")
elseif cam.CFrame.Rotation.Y > humanoid.RootPart.CFrame.Rotation.Y  then
	print("Right")
end

It’s all inside of a while loop.
Any help is appreciated!

1 Like

Hey, I think you should look into LookVectors for such an application. A VERY Basic implementation that i whipped out in 2 minutes is

local cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

while true do

	local camLook = cam.CFrame.LookVector
	local playerLook = humanoid.RootPart.CFrame.LookVector
	
	local DotVec = (playerLook:Dot(camLook))
	if DotVec > 0 and DotVec < 0.5  then
		print("Right")
	elseif DotVec >= 0.5 and DotVec <= 1 then
		print("Left")
	end
	task.wait(0.1)
end

For a Proper Understanding, i’d recommend reading through this
https://devforum.roblox.com/t/the-ultimate-guide-to-vector3cross-and-vector3dot/953984

I think I reversed the directions there lol but yea, look into the link and test some stuff and you’ll figure it out

The DotVector seems to print the same thing in both directions. It prints 0 when I look left, and 0 when I look right. I haven’t used :Dot before, but it doesn’t seem like it can really tell if I’m looking left or right, it just tells me if I’m at a right angle.

I found a solution:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.