プレイヤーの目線ではなく、自由にカメラを移動させる方法です。

  1. カメラを設置する位置にPartを置き、名前をPointにします。
  2. カメラの焦点にしたい位置にPartを置き、名前をTargetにします。
  3. 2つのPartはアンカーに設定します。
  4. StarterPlayerのStarterPlayerScriptsにLocalScriptを追加して、以下のプログラムを入力します。
local camera = game.Workspace.CurrentCamera
local target = game.Workspace:WaitForChild("Target")
local point = game.Workspace:WaitForChild("Point")

target.Transparency = 1
point.Transparency = 1

camera.CameraType = Enum.CameraType.Scriptable  -- カメラ制御をプログラムで行う
camera.CFrame = CFrame.new(point.Position,target.Position)  -- カメラの位置と向きの設定

キーボードでカメラを移動

「A」「W」「D」「S」でカメラのX、Z座標を変え、「Q」「E」でY座標を変えるプログラムです。上記のプログラムの下に配置すれば動作します。

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local pressKey = nil

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        pressKey = input.KeyCode
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        pressKey = nil
    end
end)

local moveTable = {
    A = Vector3.new(-1,0,0),
    W = Vector3.new(0,0,-1),
    D = Vector3.new(1,0,0),
    S = Vector3.new(0,0,1),
    Q = Vector3.new(0,-1,0),
    E = Vector3.new(0,1,0)
}

RunService.Heartbeat:Connect(function()
    if pressKey then
        local keyName = pressKey.Name
        local offset = moveTable[keyName]
        if offset then
            point.Position += offset
            target.Position += offset
            camera.CFrame = CFrame.new(point.Position, target.Position)
        end
    end
end)

マウスホイールの対応

UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseWheel then
        local yoffs = -input.Position.Z
        point.Position += Vector3.new(0,yoffs,0)
        target.Position += Vector3.new(0,yoffs,0)
        camera.CFrame = CFrame.new(point.Position,target.Position)
    end
end)

By schilverberch

ROBLOXでゲームを作ろう! 一緒にプログラミングを学びましょう。

コメントを残す