摄像机属性
属性 | 详情 |
---|---|
CameraType (摄像机类型) | 定义摄像机的行为(例如,固定、可编写脚本)。 |
CFrame | 表示摄像机的位置和方向。 |
FieldOfView (视野) | 控制摄像机的缩放级别。 |
Focus (焦点) | 摄像机聚焦的空间点。 |
摄像机类型
摄像机类型 | 详情 |
---|---|
Enum.CameraType.Fixed (固定) | 摄像机保持固定的位置和方向。 |
Enum.CameraType.Scriptable (可编写脚本) | 可以通过脚本控制摄像机的位置和方向。 |
Enum.CameraType.Attach (附加) | 摄像机附加到一个对象,并跟随其运动。 |
Enum.CameraType.Follow (跟随) | 摄像机跟随玩家的角色,旋转以保持其视野。 |
Enum.CameraType.Track (追踪) | 摄像机从固定位置追踪玩家的角色。 |
Enum.CameraType.Custom (自定义) | 默认摄像机行为与可定制选项。 |
设置固定摄像机
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")
自定义摄像机脚本,跟随玩家。
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)
切换摄像机
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 切换摄像机
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)