How would I rotate the camera with the head's rotation?

I’m currently attempting to make the camera rotate when the head rotates, like if I make an animation of the head going left and right I want the camera to rotate those ways too. Yet the code keeps giving me an error, help would be appreciated.

local runService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local character = game.Players.LocalPlayer.Character

runService.RenderStepped:Connect(function()
	camera.CFrame.Rotation = character.Head.CFrame.Rotation
end)

Capture

1 Like

Rotation is not settable. In your specific case what you probably want to do is copy the camera’s CFrame exactly

camera.CFrame= character.Head.CFrame

Unless you wanted something more specific?

But if he does this the camera will be in first person

Try this:

local runService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local character = game.Players.LocalPlayer.Character

runService.RenderStepped:Connect(function()
    character.Head.CFrame = CFrame.new(character.Head.Position, character.Head.CFrame.Rotation)
end)

What does that version of CFrame.new do?

Creates a new cframe value with the angles given, just like Vector3.new

That seems useful, I don’t see that in the documentation for CFrame though.

Are you sure? Look again to the documentation.
CFrame

I don’t see any overload of .new() that takes a Vector3 and a CFrame

That takes three numbers and makes a CFrame with identity rotation and those coordinates. I think I know what you mean though and it would be:
camera.CFrame = character.Head.CFrame - character.Head.Position + camera.CFrame.Position
There’s not a very good constructor for this.

You can use CFrame.new() in so many ways, you can give 3 variables for positioning X, Y, Z or give two variable one Position and one Angle or just give it a Position…

This specific overload doesn’t exist though?

Try CFrame.Angles()

— Word Limit

That didn’t work, it gave me this error:
image

You know what way the head orientation is facing, by taking the head’s orientation. Then just take the head’s lookvector, and make the camera’s orientation the same.

camera.CFrame = head.CFrame - head.CFrame.LookVector*20 -- 20 is the offset in studs, that the camera is away from the head.

CFrame.Rotation is a read-only property, as in it can only be read and not changed like that.
In order to get the rotation of a Cframe you need to use the CFrame:ToOrientation() function

local x, y, z = head.CFrame:ToOrientation() -- Returns the X, Y, Z values of the Rotation