攝影機

攝影機屬性

屬性詳情
攝影機類型定義攝影機的行為(例如,固定、可編寫腳本)。
CFrame表示攝影機的位置和方向。
視野控制攝影機的縮放級別。
焦點攝影機聚焦的空間點。

攝影機類型

攝影機類型詳情
Enum.CameraType.Fixed (固定)攝影機保持固定的位置和方向。
Enum.CameraType.Scriptable (可編寫腳本)可以通過腳本控制攝影機的位置和方向。
Enum.CameraType.Attach (附加)攝影機附加到一個對象,並跟隨其運動。
Enum.CameraType.Follow (跟隨)攝影機跟隨玩家的角色,旋轉以保持其視野。
Enum.CameraType.Track (追蹤)攝影機從固定位置追蹤玩家的角色。
Enum.CameraType.Custom (自訂)默認攝影機行為與可定制選項。

設置固定攝影機

Image 1
Roblox Studio
wait(2)
print("Camera position updating")
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Fixed
--Positioned 50 studs above the origin, looking at the origin
camera.CFrame = CFrame.new(Vector3.new(0, 50, 0), Vector3.new(0, 0, 0))
print("Camera position updated")

自訂攝影機腳本,跟隨玩家。

Image 1
Roblox Studio
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function()
	local targetPart = character:WaitForChild("HumanoidRootPart")
	local targetPosition = targetPart.Position + Vector3.new(0, 10, 10)  -- Offset above and behind the player
	camera.CFrame = CFrame.new(targetPosition, targetPart.Position) -- Camera looks at the player
end)

切換攝影機

Image 1
Roblox Studio
local function switchToCamera(cameraName)
	local camera = workspace:FindFirstChild(cameraName)
	if camera then
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		workspace.CurrentCamera.CFrame = camera.CFrame
		print("Switched to camera: " .. cameraName)
	else
		warn("Camera not found: " .. cameraName)
	end
end

local topDownCamera = Instance.new("Camera")
topDownCamera.Name = "TopDownCamera"
topDownCamera.CFrame = CFrame.new(Vector3.new(0, 50, 0), Vector3.new(0, 0, 0))
topDownCamera.Parent = workspace
switchToCamera("Camera")

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.L then
		switchToCamera("TopDownCamera")
	elseif input.KeyCode == Enum.KeyCode.P then
		workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		workspace.CurrentCamera.CFrame = workspace.Camera.CFrame
		print("Switched back to default Camera")
	end
end)

使用 TweenService 切換攝影機

Image 1
Roblox Studio
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

-- Function to switch to a specific camera by name with tweening
local function switchToCamera(cameraName)
    local camera = workspace:FindFirstChild(cameraName)
    if camera then
        workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
        
        -- Create the tween for camera transition
        local tweenInfo = TweenInfo.new(
            2, -- Time (seconds)
            Enum.EasingStyle.Quad,
            Enum.EasingDirection.Out
        )

        local goal = {}
        goal.CFrame = camera.CFrame

        local tween = TweenService:Create(workspace.CurrentCamera, tweenInfo, goal)
        tween:Play()

        print("Switched to camera: " .. cameraName)
    else
        warn("Camera not found: " .. cameraName)
    end
end

-- Create a new camera at start that looks at the origin from above (y=50)
local topDownCamera = Instance.new("Camera")
topDownCamera.Name = "TopDownCamera"
topDownCamera.CFrame = CFrame.new(Vector3.new(0, 50, 0), Vector3.new(0, 0, 0))
topDownCamera.Parent = workspace

-- Set "Camera" as the default camera
workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
workspace.CurrentCamera.CFrame = workspace.Camera.CFrame

-- Key press detection
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.L then
        switchToCamera("TopDownCamera")
    elseif input.KeyCode == Enum.KeyCode.P then
        -- Revert to the default camera instantly
        local defaultCamera = workspace:FindFirstChild("Camera")
        if defaultCamera then
            workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
            workspace.CurrentCamera.CFrame = defaultCamera.CFrame
            print("Switched back to default Camera")
        end
    end
end)

如果您覺得本教程對您有幫助,並且願意支持我的工作,請考慮請我喝杯咖啡。

非常感謝您的支持!

請我喝杯咖啡