I want the camera to only rotate on the y and x axis
Video of camera’s z axis being changed by the y and x axis
I have tried just taking the camera’s z axis and feeding it in as a negative, but the z axis still rotates slightly, not giving the intended effect.
Cannot think of any solutions, if anyone could provide sources / assistance with this matter it would be great. Thanks!
Code that has problems is below:
local function rotateCamera()
local mouse = plr:GetMouse()
local lastPosition = Vector2.new(mouse.X, mouse.Y)
local sensitivity = 0.005
while UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) do
local currentPosition = Vector2.new(mouse.X, mouse.Y)
local delta = currentPosition - lastPosition
local rotation = CFrame.Angles((delta * sensitivity).Y, (delta * sensitivity).X, 0)
cam.CFrame = cam.CFrame * rotation
lastPosition = currentPosition
wait()
end
end
Copy paste this code into a local script in starter player to test it out for yourself:
Note the code above is the problem, but you can check if you want.
local UIS = game:GetService("UserInputService")
local plr_Service = game:GetService("Players")
local plr = plr_Service.LocalPlayer
plr.CameraMode = Enum.CameraMode.LockFirstPerson
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
local cam = game.Workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
local function rotateCamera()
local mouse = plr:GetMouse()
local lastPosition = Vector2.new(mouse.X, mouse.Y)
local sensitivity = 0.005
while UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) do
local currentPosition = Vector2.new(mouse.X, mouse.Y)
local delta = currentPosition - lastPosition
local rotation = CFrame.Angles((delta * sensitivity).Y, (delta * sensitivity).X, 0)
cam.CFrame = cam.CFrame * rotation
lastPosition = currentPosition
wait()
end
end
plr:GetMouse().Button2Down:Connect(function()
rotateCamera()
end)
UIS.InputBegan:Connect(function(input)
local startCFrame = cam.CFrame
local start = os.clock()
if input.KeyCode == Enum.KeyCode.W then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame += cam.CFrame.LookVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
elseif input.KeyCode == Enum.KeyCode.A then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame -= cam.CFrame.RightVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
elseif input.KeyCode == Enum.KeyCode.S then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.S then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame -= cam.CFrame.LookVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
elseif input.KeyCode == Enum.KeyCode.D then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.D then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame += cam.CFrame.RightVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
elseif input.KeyCode == Enum.KeyCode.E then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame += cam.CFrame.UpVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
elseif input.KeyCode == Enum.KeyCode.Q then
local moveCam = true
local connected = UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
moveCam = false
end
end)
while moveCam == true do
cam.CFrame -= cam.CFrame.UpVector * (os.clock() - start)
task.wait()
end
connected:Disconnect()
end
end)