Character Camera Based Rotation Broken

Ive got a script that rotates the player on the client based on their movement and camera rotation. Basically i wanted it to slightly rotate the character with the X rotation of the camera, but im not too good at this angle stuff and the code is partly copied. Anyone know how i can fix this?

Code in a localscript:

game:GetService("UserInputService").MouseIconEnabled = false

local CamFunc = game.ReplicatedStorage.CamFunc

local Angle = 15 -- Change this value however you want

local CXM = 1

local HRootPart = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid

local OriginalC0 = HRootPart.RootJoint.C0

local Cam = game.Workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()

	local XDirection = Humanoid.MoveDirection:Dot(HRootPart.CFrame.LookVector)
	local ZDirection = -Humanoid.MoveDirection:Dot(HRootPart.CFrame.RightVector)
	
	local CX, CY, CZ = Cam.CFrame:ToEulerAnglesXYZ()
	
	local C = OriginalC0 * CFrame.Angles(math.rad(Angle*(XDirection+((-CX)*CXM))),math.rad(Angle*ZDirection),0)

	HRootPart.RootJoint.C0 = HRootPart.RootJoint.C0:Lerp(C, .2)
end)

CamFunc.OnClientInvoke = function()
	return Cam.CFrame
end

Footage:

Anyone got an idea about this?

Hello @AshOsro
I just realized that this issue occurs when CX > 1

Got an idea why this is/how to prevent it? I only noticed that it seems that the CX value is being modified by the Y rotation of the camera as well, because it gets bigger when you turn along the Y axis.

1 Like

Okay so ive edited the calculation for C and changed it to this:

local C = OriginalC0 * CFrame.Angles(math.rad(Angle*(XDirection))+((-CX)*CXM),math.rad(Angle*ZDirection),0)

which seems to be working better, but still has an issue when turning backwards that flips you upside-down. Im assuming this has to do with maths that im too dumb to understand, but idk. Ive had issues with things like this before when trying to make a player head rotate with the camera

1 Like
local test = math.clamp(CX, -0.2, 0.2)

using -test instead of -CX may work because it basically restricts the cameras influence on the characters rotation.

(Also I'm trying to figure out how to format in lua idk why I can't figure it out lol)

(figured it out)

1 Like

it seems to slightly work, but because its just clamping it, the snappy switch up in X rotation still happens when im facing backwards. Edit: I would also prefer if i didnt need to clamp it because i want it to just be based on the X rotation multiplied by a certain value that makes it rotated less.

Anyone got an idea? Iā€™m too dumb for this stuff

Try:

CX = math.clamp(CX,-.8,.8)

This doesnt fully solve the issue, it just limits the X angle. The X angle still bugs out when facing backwards. Somehow the Y angle is affecting the X angle

FINALLY FIGURED IT OUT!!!

local CX, CY, CZ = Cam.CFrame:ToOrientation()
	
	local C = OriginalC0 * CFrame.fromOrientation(math.rad(Angle*(XDirection))-CX*CXM,math.rad(Angle*ZDirection), 0)

this is what i had to change it to and it worked! I dont fully understand it all but i figured it out!

1 Like

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