【问题标题】:How to make a 2D GUI in Roblox Lua?如何在 Roblox Lua 中制作 2D GUI?
【发布时间】:2021-04-16 23:06:40
【问题描述】:

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

我正在尝试制作一个实际上是 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 吗?谢谢。

【问题讨论】:

    标签: lua 2d roblox


    【解决方案1】:

    您发布的代码仅使用任何运动的 x 分量。不知道你怎么能期待它。

    我遇到的第一个网站有这样的代码:

    如果您不停留在找到的第一个网站,网络搜索通常只会产生有用的结果。你应该选择对你有帮助的结果。

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

    播放器上显示的 2D GuiObject 的主要存储对象 屏幕。 ScreenGuis 仅在作为玩家的父级时才会显示 播放器GUI。为了确保向播放器显示 ScreenGui,它 应该作为 StarterGui 的父级,因为该服务将克隆 每个玩家加入游戏时的 PlayerGui 中的内容。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 2015-01-07
      • 2021-07-17
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2021-04-14
      • 2022-11-14
      相关资源
      最近更新 更多