I want to be able to simply move my camera in all 4 directions with the Ui buttons but the camera needs to keeps its flat orientation at all times.
No matter what I do, I can never get the orientation of the camera to stay flat at all times.
I have attached a video.
I have already tried multiple different solutions such as having a loop always set the orientation.
I tried fixing this a couple months ago and got no where so I have just come back to it to see if I could fix it with no success.
local player = game.Players.LocalPlayer
local button = script.Parent.View
local Camera = workspace.CurrentCamera
local NextCam = script.Parent.Next
local PrevCam = script.Parent.Prev
local Left = script.Parent.Directional.Left
local Right = script.Parent.Directional.Right
local Up = script.Parent.Directional.Up
local Down = script.Parent.Directional.Down
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local cameraCount = 0
local currentCameraIndex = 1
local cameras = {}
local initialOffset = CFrame.Angles(-math.rad(0), 0, 0)
for _, CameraModel in ipairs(game.Workspace.Cameras:GetDescendants()) do
if CameraModel:IsA("Model") and CameraModel.Name == "Camera" then
local cameraPart = CameraModel:FindFirstChild("Camera")
if cameraPart and cameraPart:IsA("BasePart") then
cameraCount = cameraCount + 1
local value = Instance.new("NumberValue")
value.Parent = CameraModel
value.Value = cameraCount
value.Name = "CameraNumber"
cameras[cameraCount] = cameraPart
end
end
end
local function switchToCamera(index)
if cameras[index] then
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = cameras[index].CFrame * initialOffset
Camera.FieldOfView = 120
print("Switched to Camera " .. index)
else
warn("Camera " .. index .. " not found.")
end
end
local Viewing = false
local function synchronizeCamera()
while Viewing do
if cameras[currentCameraIndex] then
Camera.CFrame = cameras[currentCameraIndex].CFrame
end
wait()
end
end
local function rotateCamera(direction)
local rotationSpeed = math.rad(1)
if cameras[currentCameraIndex] then
local rotation = CFrame.new()
if direction == "Up" then
rotation = CFrame.Angles(-rotationSpeed, 0, 0)
elseif direction == "Down" then
rotation = CFrame.Angles(rotationSpeed, 0, 0)
elseif direction == "Right" then
rotation = CFrame.Angles(0, -rotationSpeed, 0)
elseif direction == "Left" then
rotation = CFrame.Angles(0, rotationSpeed, 0)
end
cameras[currentCameraIndex].CFrame = cameras[currentCameraIndex].CFrame * rotation
end
end
local function startRotate(direction)
local connection
connection = RunService.RenderStepped:Connect(function()
if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
rotateCamera(direction)
else
connection:Disconnect()
end
end)
end
Up.MouseButton1Down:Connect(function()
startRotate("Up")
end)
Down.MouseButton1Down:Connect(function()
startRotate("Down")
end)
Right.MouseButton1Down:Connect(function()
startRotate("Right")
end)
Left.MouseButton1Down:Connect(function()
startRotate("Left")
end)
button.MouseButton1Click:Connect(function()
print("Clicked")
if Viewing then
Viewing = false
NextCam.Visible = false
PrevCam.Visible = false
Camera.CameraType = Enum.CameraType.Custom
Camera.FieldOfView = 70
print("Camera changed to Custom")
else
Viewing = true
NextCam.Visible = true
PrevCam.Visible = true
switchToCamera(currentCameraIndex)
synchronizeCamera()
end
end)
NextCam.MouseButton1Click:Connect(function()
if Viewing then
currentCameraIndex = currentCameraIndex % cameraCount + 1
switchToCamera(currentCameraIndex)
end
end)