Character tilts when strafing or moving backwards

Recently, I attempted on making the character face the same direction as the camera. It worked, only problem is that the character tilts when strafing or moving backwards.

GIF

https://gyazo.com/e5f5f7db59fb826a013e9e1e901db985

Relevant Code
 game:GetService("RunService").RenderStepped:Connect(function()
    local rx, ry, rz = Camera.CFrame:ToOrientation()
 	Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame:Lerp(CFrame.new(Character.HumanoidRootPart.CFrame.Position) * CFrame.fromOrientation(0, ry, 0), .5)
 end)

Note: I am lerping this to make it smoother, not lerping it doesn’t change much and instead makes the camera buggy when strafing or moving backwards.

Angles and Orientation are a pain to work with. If you’re alright with dumping the orientation math, I have the solution for you. :slight_smile:

local RS = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

local cam = workspace.CurrentCamera

RS.RenderStepped:Connect(function()
	local origin = root.CFrame.Position
	local look = cam.CFrame.LookVector
	local flat = Vector3.new(1,0,1)
	root.CFrame = root.CFrame:Lerp(CFrame.new(origin, origin + (look * flat)), .5)
end)

This code simply uses the second argument of CFrame.new() to create a LookAt function.
We basically just take the origin (aka the current root CFrame) and make it look at the origin + the LookVector of the camera.
We also multiplied the LookVector by (1,0,1), we did this because it removes the Y value so that the character only follows the X and Z of the camera.

CFrame Data reference:
https://developer.roblox.com/en-us/api-reference/datatype/CFrame

The important info on this page is this:

Now, you could easily replace CFrame.new() with CFrame.lookAt(), but I personally prefer the .new() function. Both work equally in your case.

I hope this helps! :slight_smile:

1 Like

This sadly didn’t help. The problem still occurs and it behaves exactly like my original code.

When tested on a blank baseplate the script works fine, the problem most likely lies within the code affecting the camera. Do you have the code that updates the camera’s position readily available? if so I can take a glance over it and check for any gleaming problems! :slight_smile:

The only scripts in the place are modules that have nothing to do with the camera. Aside from that, I’ve only messed with the cameraoffset and the mousebehavior.

What if you try turning off Humanoid.AutoRotate? The code @Xitral provided works perfectly for me.

1 Like

That actually worked, I seriously hate writing an entire topic just for it to get solved with 1 line of code. Either way, thank you!

No problems. Humanoid.AutoRotate can cause problems with custom character rotation, so it’s always best to turn it off if not needed. Also solves possible character stutters when using over-the-shoulder camera systems.

2 Likes