如何在 Roblox Lua 中制作 2D GUI?

我正在尝试为我的 Roblox 游戏制作一个 2D GUI,但是我搜到的代码只是将相机设置在侧面角度,使其看起来像是 2D。但实际上,它只是从一个角度看起来是 2D 的 3D。

我正在尝试制作一个真正的 2D GUI,就像你在这张图片中看到的那样。 A Roblox game that has a 2D GUI.

我找到的第一个网站上有这个代码:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

player.CharacterAdded:Wait()

player.Character:WaitForChild("HumanoidRootPart")

camera.CameraSubject = player.Character.HumanoidRootPart
camera.CameraType = Enum.CameraType.Attach
camera.FieldOfView = 40

local RunService = game:GetService("RunService")

local function onUpdate()
    if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
        camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position) * CFrame.new(0,0,30)
    end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onUpdate)

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false
local leftValue, rightValue = 0, 0

local function onLeft(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        leftValue = 1
    elseif inputState == Enum.UserInputState.End then
        leftValue = 0
    end
end

local function onRight(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        rightValue = 1
    elseif inputState == Enum.UserInputState.End then
        rightValue = 0
    end
end

local function onJump(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        jumping = true
    elseif inputState == Enum.UserInputState.End then
        jumping = false
    end
end

local function onUpdate()
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        if jumping then
            player.Character.Humanoid.Jump = true
        end
        local moveDirection = rightValue - leftValue
        player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
    end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "a", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "d", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Jump", onJump, true, "w", Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

并且它给出了这个结果:https://youtu.be/BEal4GHbKss

请问能否帮我在 Roblox 中制作一个 2D GUI?谢谢。

点赞
用户2858170
用户2858170

你发布的代码仅使用了运动的 x 轴分量。不确定你如何期望从中获得其他结果。

我找到的第一个网站上有这个代码:

通常只有当你不止停留在第一个找到的网站时,Websearch 才会产生有用的结果。你应该选择对你有帮助的结果。

使用 ScreenGUI https://developer.roblox.com/en-us/api-reference/class/ScreenGui

2D GuiObject 显示在玩家屏幕上的主要存储对象。如果将 ScreenGui 父级化到玩家的 PlayerGui,ScreenGuis 将仅在那里显示。为确保 ScreenGui 显示给你的玩家,它应该父级化到 StarterGui,因为该服务会在玩家加入游戏时克隆它的内容到每个玩家的 PlayerGui 中。

2021-01-12 08:54:37