Calculate lookvector out of camera cframe

how do i convert the camera cframe into a lookvector for the characters root part? simple as that. i only want the y axis so they look around but not up/down. im aware there are other posts on this but they arent helpful or descriptive. its almost 3 in the morning here and im literally pulling out hair over this because my character is spazzing when i make a wrong move and i don’t know how to do math in the first place. anything is appreciated.

also, my attempts come from trying to recycle code to make the characters head look with the camera. i didn’t even write the code, i took it from a game i helped a friend clean up, and it works perfectly for its own purpose.

anyways, the code:
local root = chr:WaitForChild(“HumanoidRootPart”)
local hum = chr:WaitForChild(“Humanoid”)
local camDir = root.CFrame:ToObjectSpace(newCF).LookVector
lookDir = CFrame.new(0, 0, 0) * CFrame.Angles(0, -math.atan(camDir.X),
math.asin(camDir.Z)).LookVector
–lookDir eventually becomes the lookvector through proper means (position + lookDir)

and the result:
https://gyazo.com/ce37376a0c3dec7cc41a8940ccb78e76 (head movement is covered elsewhere and doesn’t interfere)

pls i need sleep

From your gyazo link, it seems as though you’re just trying to get the character to rotate based on the camera’s rotation? If so:

local Rot = math.atan2(-Camera.CFrame.lookVector.X, -Camera.CFrame.lookVector.Z)
local newLookFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0, Rot, 0)
1 Like

i’m still in bed atm, but i’ll try it first thing and mark as solution if it works. thank youu

You can multiply vectors to get the lookvector’s projection onto the XZ plane.

lookDir = (camDir * Vector3.new(1, 0, 1)).Unit
root.CFrame = CFrame.new(root.Position, root.Position + lookDir)
1 Like

yeah, you’re a life-saver. ooga booga tired brain cant do math, thanks for having my back lmao

2 Likes