カメラ

カメラプロパティ

プロパティ詳細
CameraType (カメラタイプ)カメラの動作を定義します (例: 固定、スクリプト可能)。
CFrameカメラの位置と方向を表します。
FieldOfView (視野角)カメラのズームレベルを制御します。
焦点カメラが焦点を合わせる空間のポイント。

カメラタイプ

カメラタイプ詳細
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)