我正在尝试使用教程来创建自上而下的相机,以下是链接:https://education.roblox.com/en-us/resources/arcade-game-top-down-camera
这是我的代码:
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local CAMERA_OFFSET = Vector3.new(-1,20,0)
camera.CameraType = Enum.CameraType.Scriptable
local function onRenderStep()
if player.Character then
local playerPosition = player.Character.HumanoidRootPart.Position
local cameraPosition = playerPosition + CAMERA_OFFSET
camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
print("player position = " .. playerPosition.X .. " : " .. playerPosition.Y .. " : " .. playerPosition.Z)
print("camera position = " .. cameraPosition.X .. " : " .. cameraPosition.Y .. " : " .. cameraPosition.Z)
end
end
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)
这将相机按预期设置在播放器上方。但是,当我将CAMERA_OFFSET的值更改(例如,将Y从20更改为200)并重新启动游戏时,相机未将cpomparpared的位置更改为先前值。调试播放器和摄像机的位置,可以看到偏移已正确添加:
player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05
camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05
player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05
camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05
player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05
camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05
我的最终目标是将相机固定在播放器上方(现在仍然可以通过鼠标滚动上下移动它,但是首先要了解如何将其设置在正确的高度。
如果您放一个
wait()
在脚本的顶部,它将完全按照您的要求进行。
我不确定这是否是正确的方法,或者是否应该等待其他对象完成加载。没有任何类型的错误消息,我也找不到任何文档。