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
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.
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
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.
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
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!