Small CFrame Problem

Problem
I want the camera to be facing the front of the player (disregarding their orientation), I don’t want the camera looking exactly at the player’s position, but just the front of the player. I don’t know how to do it…

I’m not an expert when it comes to using CFrames, but I’ve done enough reading to understand how it works. I know if I want the camera to be on the right side of the player I have to do

Camera.CFrame = CFrame.new(Player.Head.Position + (Player.Head.CFrame.RightVector*5))

However I require some help on figuring out how to make the front of the camera face the player direction (not position) when the player turns.

Disclaimer
I’m not trying to constantly change the camera position or orientation with a loop or anything. The camera just spawns in and that’s legit it. I just want the camera to be facing in the right direction when it spawns, which I don’t know how to code.

Normal Example Image

Example image of what happens when the player turns (don’t want this)

Example Image of the way I want it to be when the player turns

Use CFrame.lookAt, which creates a CFrame at the first position that is looking at the second one.

Camera.CFrame = CFrame.lookAt(Player.Head.Position + (Player.Head.CFrame.RightVector*5), Player.Head.Position)
1 Like

Is that really the only way to do it? I want to avoid doing it this way cause it will change the orientation of the camera completely. I don’t want it to looking directly at their position because I plan on placing the camera a stud ahead of the player, I want it to be kinda like a 2D camera. I’m sorry if I’m confusing people.

1 Like

If I’m understanding correctly, you may be able to start the camera facing them and then move it a stud ahead so you have the correct orientation. Not sure if there is a better way to do it.

1 Like

If you want it to always face a different position, multiply it by the angle you want.

Camera.CFrame = CFrame.new(Player.Head.Position + (Player.Head.CFrame.RightVector*5))
	* CFrame.Angles(0, math.pi, 0) -- 180deg

If that isn’t right, move the math.pi to another zero and try again. Try dividing by 2 also.

1 Like

I’m sorry for the late response, I’ve tried this method before and it ended up having no changes. If I were to spawn the camera and my character orientation is facing 180 degrees then the camera back face will be facing the player, instead of it’s front face.

CFrame.new(character:WaitForChild("Head").Position + (character:WaitForChild("Head").CFrame.RightVector*5) + (character:WaitForChild("Head").CFrame.lookVector*2)) * CFrame.Angles(math.rad(0),math.pi/2,math.rad(0))

Video

Okay, if you want it relative to the head do this:

-- vars
local runS = game:GetService("RunService")
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local head = char:WaitForChild("Head")
-- code
cam.CameraType = Enum.CameraType.Scriptable
local offset = CFrame.new(5, 0, -2) * CFrame.Angles(0, math.pi/2, 0)
runS:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, function(delta)
	cam.CFrame = head.CFrame * offset
end)
1 Like

THANK YOU! CFrames are tricky to me at least. I appreciate your help and patience. :sweat_smile:

1 Like

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