将用户输入连接到动作使用户能够更加直观和更好地控制您体验的功能。在本教程中,您将把一个重载动作绑定到一个特定的按键。
开始
本教程使用在 创建玩家工具 中创建的 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 函数,重载动画完成后再重新激活它。