I am currently having an issue with the player rotation giving weird results when I try to calculate it relative to the mouse LookVector. Here’s a video showcasing the issue:
It looks alright as I face one direction, but the moment I switch to a different direction it doesn’t seem to work anymore. Not sure what is going on, but is most likely on how I am calculating the mouse LookVector. The Y-Axis tilting seems to be working regardless however…
Here is the relevant code:
local RunService = game:GetService("RunService")
local camera: Camera = workspace.CurrentCamera
local character: Model? = game.Players.LocalPlayer.Character
local rootMotor: Motor = character:WaitForChild("UpperTorso").Waist
local mouse: Mouse = game.Players.LocalPlayer:GetMouse()
local CFNew, CFAng, asin, clamp = CFrame.new, CFrame.Angles, math.asin, math.clamp
local Y_CLAMP_MIN, Y_CLAMP_MAX = -0.65, 0.65
local X_CLAMP_MIN, X_CLAMP_MAX = -0.8, 0.8
local ROOT_Y_OFFSET = rootMotor.C0.Y
local LERP_INDEX = 0.15
RunService.RenderStepped:Connect(function()
local cameraDirection = mouse.Hit.LookVector
if rootMotor then
local final = CFNew(0, ROOT_Y_OFFSET,0) * CFAng(0, -asin(cameraDirection.X), 0)
* CFAng(asin(cameraDirection.Y), 0, 0)
rootMotor.C0 = rootMotor.C0:Lerp(final, LERP_INDEX)
end
end)
Is it having issues reading - or + angles and putting them into the formula? Or is it because the torso values are in ObjectSpace and you are reading a Mouse WorldSpace value?
So I dont have an exact solution but I can give some advice from experience cause I remember a similar thing happening to me about a year-ish ago
For some reason when you go to the other side it will flip the - and + so I just stopped relying on CFrames and went with the orientation and then modified it into a CFrame – Its a really hacky solution but something similar should work
This seems like a “object/world space” issue, looking at the right side from a different angle such as looking behind makes the torso tilt to the left side instead.
Perhaps make the cameraDirection relative to the player RootPart’s cframe, which, can be done by doing: local cameraDirection = (plr.Character.HumanoidRootPart.CFrame.Rotation * mouse.Hit.LookVector).
If the previous method didn’t work, use mouse.Hit.Position to get the direction between the player RootPart and the hit position. local cameraDirection = (mouse.Hit.Position - plr.Character.HumanoidRootPart.Position).Unit.
Also, methods or functions like CFrame.new are optimized, so it’s no use setting them as local variables. Though I really recommend using Vector3.zero instead of Vector3.new(0,0,0) as a local variable for future trigonometry use in your code.