Character rotation with camera

I was following this tutorial: The First Person Element Of A First Person Shooter and I was interested in the part where the character looked up and down with the camera, like in games like jailbreak. I also wanted to make the character look sideways as well, but I was unable to quite achieve that.

I tried using the same method as in the tutorial with the X angle, but it only worked properly when the character was aligned along the x-axis, and vice versa for when I tried it with the z-axis. I got a really weird result when I tried adding both the z and x angles, so that’s not a solution.

My current code:

--I believe these are the default C0 Cframes 
--i got them from the tutorial above
local neckC0 = CFrame.new(0, 0.8, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1); 
local waistC0 = CFrame.new(0, 0.2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1);

runService.Heartbeat:Connect(function()
	local neckJoint = localChar.Head.Neck
	local waistJoint = localChar.UpperTorso.Waist
	
	local yAngle = math.asin(camera.CFrame.LookVector.Y)
	local xAngle = math.asin(camera.CFrame.LookVector.X)
	
	neckJoint.C0 = neckC0 * CFrame.Angles(yAngle * 0.5, -(xAngle) * 0.5, 0)
	waistJoint.C0 = waistC0 * CFrame.Angles(yAngle * 0.5, -(xAngle)* 0.5, 0)
end)

What it currently does:
robloxapp-20210920-2242309.wmv (2.6 MB)

I tried a multitude of other things as well, but none of them worked as desired. I feel like character rotation also plays a part, but I’ve been unable to figure out how. If some mathematical magician could help me get this right, it would be much appreciated.
Cheers!

3 Likes

It’s different because the up/down angle doesn’t care about your character (up is always up), while the left/right does care (“forward” changes depending on where your character is looking).

One solution would be to get the camera’s lookvector in the space of the character and use that instead:

	local objectSpaceLookVector = localChar.HumanoidRootPart.CFrame:VectorToObjectSpace(camera.CFrame.LookVector)
	local xAngle = math.asin(objectSpaceLookVector.X)
2 Likes