将用户输入连接到动作可以让用户对游戏功能有更好和更直观的控制。在本教程中,您将把重新加载动作绑定到特定的键。
开始
本教程使用在 创建玩家工具 中创建的 Blaster 工具。您可以按照这些说明创建工具,或者下载 Blaster 模型并将其插入到 StarterPack 中。
模型可以添加到您的库存中,以便在任何游戏之间使用。要将模型添加到您的游戏中:
- 在浏览器中,打开 模型 页面,点击 获取 按钮。这会将模型添加到您的库存中。
- 确保下拉菜单设置为 我的模型。
- 选择 Blaster 模型以将其添加到游戏中。
创建动作处理程序
首先,您需要一个函数来处理检测到的用户输入。
打开 Blaster 内部的 ToolController LocalScript。

创建一个变量来存储动作的名称。
local tool = script.Parentlocal RELOAD_ACTION = "reloadWeapon"local function toolEquipped()tool.Handle.Equip:Play()endlocal function toolActivated()tool.Handle.Activate:Play()endtool.Equipped:Connect(toolEquipped)tool.Activated:Connect(toolActivated)创建一个名为 onAction 的函数,该函数接收三个参数:actionName、inputState 和 inputObject。这将是当检测到用户输入时运行的函数。
local tool = script.Parentlocal RELOAD_ACTION = "reloadWeapon"local function onAction(actionName, inputState, inputObject)endlocal function toolEquipped()tool.Handle.Equip:Play()end在函数内部,检查给定的 actionName 是否与重新加载动作名称匹配,并确保 inputState 为 UserInputState.Begin(开始状态)。这很重要,因为该函数将在每次 inputState 更改时运行,但重新加载只需要发生一次。
local function onAction(actionName, inputState, inputObject)if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin thenendend为了让用户重新加载时更明显,将工具的 TextureId 更改为 "rbxassetid://6593020923" 一段时间,然后再将其更改回原始值 "rbxassetid://92628145"。
local function onAction(actionName, inputState, inputObject)if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin thentool.TextureId = "rbxassetid://6593020923"task.wait(2)tool.TextureId = "rbxassetid://92628145"endend
绑定动作
ContextActionService 可用于 绑定 函数到特定输入,使用 BindAction 函数,该函数接受多个参数:
- 动作的名称。
- 处理该动作的函数(也称为“回调”)。
- 是否应显示触摸按钮。
- 任何数量的 Enum.KeyCodes 以检测并与该动作关联。
KeyCodes 是表示不同输入按钮的值,例如键盘键或控制器按钮。完整的代码列表可以在 这里 找到。
在脚本顶部获取 ContextActionService。
local ContextActionService = game:GetService("ContextActionService")local tool = script.Parentlocal RELOAD_ACTION = "reloadWeapon"在 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 thentool.TextureId = "rbxassetid://6593020923"task.wait(2)tool.TextureId = "rbxassetid://92628145"endendlocal function toolEquipped()ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)tool.Handle.Equip:Play()end通过装备工具并按下键盘上的 R 键进行游戏测试。背包图标应短暂更改为等待符号,以表示武器正在重新加载:
解绑动作
当用户卸下工具时,需要 解绑 动作,以便他们无法在没有装备工具的情况下重新加载。
创建一个名为 toolUnequipped 的新函数,并调用 UnbindAction,传递动作名称。
local function toolEquipped()ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)tool.Handle.Equip:Play()endlocal function toolUnequipped()ContextActionService:UnbindAction(RELOAD_ACTION)endlocal function toolActivated()tool.Handle.Activate:Play()endtool.Equipped:Connect(toolEquipped)tool.Activated:Connect(toolActivated)将 toolUnequipped 函数连接到 Unequipped 事件,以便在事件触发时运行该函数。
local ContextActionService = game:GetService("ContextActionService")local tool = script.Parentlocal RELOAD_ACTION = "reloadWeapon"local function onAction(actionName, inputState, inputObject)if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin thentool.TextureId = "rbxassetid://6593020923"task.wait(2)tool.TextureId = "rbxassetid://92628145"endendlocal function toolEquipped()ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)tool.Handle.Equip:Play()endlocal function toolUnequipped()ContextActionService:UnbindAction(RELOAD_ACTION)endlocal function toolActivated()tool.Handle.Activate:Play()endtool.Equipped:Connect(toolEquipped)tool.Unequipped:Connect(toolUnequipped)tool.Activated:Connect(toolActivated)进行游戏测试以确认一切正常。您应该能够在装备工具时重新加载,但在卸下时无法重新加载。
您的重新加载动画现在完成 - 为了增加挑战,尝试在每次发射时倒计时弹药计数器。然后,当枪没有弹药时,可以停用 toolActivated 函数,等到重新加载动画完成后再重新激活它。