偵測使用者輸入

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

將使用者輸入連接到行動可讓使用者對體驗功能的控制更好且更直覺。在本教學中,您將綁定重新載入行動到特定鍵。

開始使用

本教學使用在 創建玩家工具 中創建的 爆破器 工具。您可以遵循這些指示創建工具,或可以下載 爆破器 模型並插入 新手包

模型可以添加到您的庫存中,在任何體驗之間使用。要將模型添加到您的體驗中:

  1. 在瀏覽器中,開啟 模型 頁面,點擊 獲取 按鈕。這將模型添加到您的道具欄中。
  2. 在工作室中,前往 視圖 標籤,然後單擊 工具箱
  3. 在工具箱窗口中,單擊 庫存 按鈕。然後,確保下拉選單位在 我的模型 上。
  4. 選擇 爆破器 模型,將其添加到體驗中。

創建一個行動處理器

首先,當使用者輸入偵測到時,你需要一個功能來處理。

  1. 在發射器內開啟 工具控制器 LocalScript

  2. 建立變量來儲存行動作的名稱。


    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function toolEquipped()
    tool.Handle.Equip:Play()
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Activated:Connect(toolActivated)
  3. 創建名為 onAction 的功能,接收三個參數:actionNameinputStateinputObject。這將是在偵測到使用者輸入時運行的功能。


    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    end
    local function toolEquipped()
    tool.Handle.Equip:Play()
    end
  4. 在函數內,檢查指定的 actionName 與重新載行動名稱匹配,並確保 inputStateUserInputState.Begin (初始狀態)。這很重要,因為函數會在每次 inputState 變更時運行,但重新載只需要發生一次。


    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    end
    end
  5. 當使用者重新載入時,將工具的 TextureId 變更為 "rbxassetid://6593020923" 一會兒,然後將其變更回原始值 "rbxassetid://92628145"


    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end

綁定行動作

ContextActionService 可以用來 綁定 一個函數到特定輸入,使用 BindAction 函數,該函數接受多個參數:

  • 行動作名稱
  • 處理行動的功能(也稱為「回調」)
  • 是否應顯示觸摸屏按鈕
  • 任何數量的 Enum.KeyCodes 以偵測並與動作動聯繫。

鍵碼是代表不同輸入按鈕的值,例如鍵盤按鈕或控制器按鈕。完整的代碼列表可用 here

  1. 在腳指令碼頂部獲得 ContextActionService


    local ContextActionService = game:GetService("ContextActionService")
    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
  2. toolEquipped 函數內,呼叫 BindAction 並通過以下參數傳送:

    • 行動名稱 ( RELOAD_ACTION )
    • 行動處理器 ( onAction )
    • 一個值來創建觸摸按鈕(true)
    • 按下鍵以偵測(Enum.KeyCode.R

    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end
    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
  3. 裝備工具並按下您鍵盤上的 R 鍵來進行測試。背包圖示應暫時變更為等待符號,以警告武器正在重新載入:

取消行動的綁定

當使用者卸下工具時,行動必須是 解除綁定 ,這樣他們才能夠在沒有工具裝備的情況下重新載入。

  1. 創建一個名為 toolUnequipped 的新功能,並呼叫 UnbindAction ,通過行動名稱傳送。


    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
    local function toolUnequipped()
    ContextActionService:UnbindAction(RELOAD_ACTION)
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Activated:Connect(toolActivated)
  2. toolUnequipped 功能連接到 Unequipped 事件,因此當事件發生時,功能將運行。


    local ContextActionService = game:GetService("ContextActionService")
    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end
    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
    local function toolUnequipped()
    ContextActionService:UnbindAction(RELOAD_ACTION)
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Unequipped:Connect(toolUnequipped)
    tool.Activated:Connect(toolActivated)
  3. 進行測試以確認所有都正確運作。當裝備工具時,您應該能夠重新載入,但不是當它未裝備時。

你的重新載入動畫現已完成 - 為了額外的挑戰,每次發射爆破器時,試著倒數計時一個彈藥計數器。當槍沒有彈藥時,您可以停用 toolActivated 功能,然後在重新載入動畫完成後重新啟用它。