You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在New Input System中编写绑定不同按钮的第二个攻击函数?

用Unity新输入系统实现第二个攻击动作(紫色骷髅法术)

1. 先在Input Actions资源中添加新动作

打开你正在使用的Input Actions资产(比如PlayerInputActions.inputactions):

  • 在对应的输入映射(比如Player)下新增一个Button类型的动作,命名为SummonPurpleSkull(或者你喜欢的自定义名称)
  • 绑定右侧向下按键:键盘端选Key -> Down Arrow (Right),手柄端选Dpad Right -> Down,可根据你的游戏操作需求调整

2. 代码中关联新动作并实现法术逻辑

假设你已经有处理普通攻击的脚本,直接扩展即可:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerAttack : MonoBehaviour
{
    private PlayerInputActions _inputActions;
    [SerializeField] private GameObject purpleSkullPrefab; // 你的紫色骷髅预制体
    [SerializeField] private float skullCooldown = 2f; // 法术冷却时间
    private float _lastCastTime;

    private void Awake()
    {
        _inputActions = new PlayerInputActions();
        // 绑定已有的普通攻击
        _inputActions.Player.Attack.performed += _ => Attack();
        // 绑定新的骷髅法术触发动作
        _inputActions.Player.SummonPurpleSkull.performed += _ => SummonPurpleSkull();
    }

    private void OnEnable() => _inputActions.Player.Enable();
    private void OnDisable() => _inputActions.Player.Disable();

    // 你已有的普通攻击方法
    private void Attack()
    {
        Debug.Log("触发普通攻击");
        // 你的原有攻击逻辑
    }

    // 新增的紫色骷髅法术释放方法
    private void SummonPurpleSkull()
    {
        // 冷却判断,避免短时间内连续触发
        if (Time.time - _lastCastTime < skullCooldown) return;
        _lastCastTime = Time.time;

        Debug.Log("释放紫色骷髅法术");
        // 实例化骷髅预制体的示例逻辑
        Instantiate(purpleSkullPrefab, transform.position + transform.forward, Quaternion.identity);
        // 这里可以补充特效播放、音效触发等逻辑
    }
}

3. 可视化绑定替代方案(如果你用PlayerInput组件)

如果你的玩家对象上挂了PlayerInput组件:

  • 在组件的Actions字段选择你的Input Actions资产
  • 展开Events,点击Add Event,选择SummonPurpleSkull动作
  • 拖拽你的PlayerAttack脚本到事件槽,选择SummonPurpleSkull()方法即可

内容的提问来源于stack exchange,提问作者Manuel Neto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:52:28