How do I set CFrame orientation to be straight?

I have an avatar editor and I’m trying to set it so the camera goes in front of the player, however there is one problem.
The camera is slightly tilted, I’m assuming it’s because I’m basing it off of the head’s CFrame but I have no idea how to fix this.

Can someone please let me know how to change this code so it’ll be straight.

cam.CFrame = plr.Character.Head.CFrame * CFrame.new(0, 0, -5) * CFrame.Angles(0, math.pi, 0)

1 Like

Can you provide me the script on when the bug is happening?
This will be helpful.

1 Like

Hi, thanks for your reply, the code is in the post

local plr = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = plr.Character.Head.CFrame * CFrame.new(0, 0, -5) * CFrame.Angles(0, math.pi, 0)
1 Like

Is the script local or server?

1 Like

It is a localscript since it’s moving the camera

1 Like

Ok.
Where did you put the localscript?

1 Like

When I say where, I mean in Explorer.

1 Like

StarterGui. It’s working but I just wanted to know how to fix it so the camera is straight.

1 Like

I think I knew how to fix this. Give me one moment while I test your problem.

1 Like

Did you check the ‘CameraType’?
If not, I think you should set it to Scriptable.

It is set to scriptable. The script is working, I just want to make the camera straight…

1 Like

It’s probably because the head’s CFrame isn’t straight when applying the CFrame. You could do this relatively easily by just negating the X and Z axes of the camera’s orientation.

local headCFrame = head.CFrame
local offset = head.CFrame * CFrame.new(0, 0, -5) * CFrame.Angles(0, math.pi, 0)
local _, y = offset:ToOrientation()
local finalCFrame = CFrame.new(offset.Position) * CFrame.Angles(0, y, 0)
cam.CFrame = finalCFrame
2 Likes

Thanks for the reply but there seems to be an error

1 Like

Oops my bad, try CFrame.new(offset.Position) * CFrame.Angles(0, y, 0) instead.

2 Likes

Thank you! Works perfectly now!!

1 Like

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