Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Camera rotating around Z without explicitly adding to Z rotation, why?

Asked by
fpnky 0
4 years ago
camera.CameraType = Enum.CameraType.Scriptable
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
camera.FieldOfView = 80
camera.CameraSubject = humanoid

local rot = Vector3.new(0, 0, 0)
local camOffset = Vector3.new(2, 1, 5)

local function onRenderStepped(dt)
    local deltaMouse = UserInputService:GetMouseDelta()
    rot = rot + Vector3.new(deltaMouse.Y, -deltaMouse.X, 0)/100
    --Line that sets the cameras CFrame
    camera.CFrame = (CFrame.new(humanoidRootPart.Position) * CFrame.Angles(rot.X, rot.Y, 0)) * CFrame.new(camOffset)
    local x,y,z = camera.CFrame:ToOrientation()
    humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0)
end

The line under the comment is the one that changes the rotation, it seems to do what I want (which is rotate the camera when my mouse moves, like shift lock) but when I move my mouse (in any direction) it applies some small amount of rotation around the Z axis, which skews the camera making it look very weird even though I don't add to the axis. I checked all the CFrames by printing CFrame:ToOrientation() however both CFrame.new(humanoidRootPart.Position) and CFrame.new(camOffset) both output -0, 0, 0 which suggests to me that that isn't the problematic part (but I could be wrong and maybe the decimal precision isn't high enough, so it just rounds to 0).

If you have any suggestions on how to improve the script please put them below, or a better way to do this, anything helps.

Answer this question