Im trying to make script that does exactly what the server camera does so when you hold right click the camera rotates around. So far I have a script that kinda does this but its really wonky.
Desired Output:
What I have currently:
Script:
local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = workspace.CameraPart.CFrame
mouse.Button2Down:Connect(function()
uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end)
mouse.Button2Up:Connect(function()
uis.MouseBehavior = Enum.MouseBehavior.Default
end)
function updatecam()
local delta = uis:GetMouseDelta()
local x = delta.X
local y = delta.Y
cam.CFrame = cam.CFrame * CFrame.Angles(math.rad(-y),math.rad(-x),math.rad(0))
end
rs:BindToRenderStep("MeasureMouseMovement",Enum.RenderPriority.Input.Value,updatecam)
You can usually very simply solve this issue by replacing CFrame.Angles with CFrame.FromEulerAnglesYXZ
The reason this works is because cframe.angles calculates up and down before it calculates side to side, and when you rotate an object up and down first, the side to side is now angled upwards and therefore is angled and gross
FromEulerAnglesYXZ calculates side to side first, hence the YXZ instead of XYZ
So i replaced cam.CFrame = cam.CFrame * CFrame.Angles(math.rad(-y),math.rad(-x),math.rad(0))
with cam.CFrame = cam.CFrame * CFrame.fromEulerAnglesYXZ(math.rad(-y),math.rad(-x),math.rad(0))
There was another small underlying problem I overlooked
The up down was still being accounted for because you were just multiplying the angle on
This code should work:
local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = workspace.CameraPart.CFrame
mouse.Button2Down:Connect(function()
uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end)
mouse.Button2Up:Connect(function()
uis.MouseBehavior = Enum.MouseBehavior.Default
end)
local tx = 0
local ty = 0
function updatecam()
local delta = uis:GetMouseDelta()
tx += delta.X
ty += delta.Y
cam.CFrame = workspace.CameraPart.CFrame * CFrame.fromEulerAnglesYXZ(math.rad(-ty),math.rad(-tx),math.rad(0))
end
rs:BindToRenderStep("MeasureMouseMovement",Enum.RenderPriority.Input.Value,updatecam)
Im not sure if its exactly what you want but it should be pretty easy to do whatever you want with it, its easier to clamp now also if you want to do that