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

Unity新输入系统实现四方向攻击动画的技术求助

实现四方向专属攻击动画方案

一、Animator控制器设置

首先在你的Animator中添加四个布尔型参数:

  • IsAttackingLeft
  • IsAttackingRight
  • IsAttackingDown
  • IsAttackingUp

然后为每个方向创建对应的攻击动画,并设置过渡规则:

  • 从待机/移动状态到对应攻击状态的过渡条件为:对应方向的攻击布尔值为true
  • 在每个攻击动画的最后一帧添加动画事件,调用后续脚本中写的ResetAttackStates函数,确保攻击状态能被重置,允许下次触发攻击

二、脚本修改与实现

1. 添加状态记录变量

在脚本顶部添加以下变量,用于记录当前面向方向和攻击冷却:

// 记录当前面向的方向(默认可设为向下,或根据初始状态调整)
private Vector2 currentFacingDirection = Vector2.down;
// 攻击冷却时间,防止连续触发攻击
[SerializeField] private float attackCooldown = 0.3f;
private bool canAttack = true;

2. 更新移动时的方向记录

修改FixedUpdate中设置移动动画的部分,在每次设置移动布尔值的同时更新currentFacingDirection:

if(movementInput.x < 0) // 向左移动
{
    animator.SetBool("IsMovingLeft", true);
    animator.SetBool("IsMovingDown", false);
    animator.SetBool("IsMovingRight", false);
    animator.SetBool("IsMovingUp", false);
    // 更新面向方向
    currentFacingDirection = Vector2.left;
}
else if(movementInput.x > 0) // 向右移动
{
    animator.SetBool("IsMovingLeft", false);
    animator.SetBool("IsMovingDown", false);
    animator.SetBool("IsMovingRight", true);
    animator.SetBool("IsMovingUp", false);
    currentFacingDirection = Vector2.right;
}
else if(movementInput.y < 0) // 向下移动
{
    animator.SetBool("IsMovingLeft", false);
    animator.SetBool("IsMovingDown", true);
    animator.SetBool("IsMovingRight", false);
    animator.SetBool("IsMovingUp", false);
    currentFacingDirection = Vector2.down;
}
else if(movementInput.y > 0) // 向上移动
{
    animator.SetBool("IsMovingLeft", false);
    animator.SetBool("IsMovingDown", false);
    animator.SetBool("IsMovingRight", false);
    animator.SetBool("IsMovingUp", true);
    currentFacingDirection = Vector2.up;
}

3. 实现OnFire攻击逻辑

补全OnFire函数,根据当前面向方向触发对应攻击动画,同时加入冷却逻辑:

void OnFire(InputValue inputValue)
{
    if(!canAttack) return;

    // 先重置所有攻击状态,避免多个攻击状态同时触发
    ResetAttackStates();

    // 根据当前面向方向设置对应攻击布尔值
    if(currentFacingDirection == Vector2.left)
    {
        animator.SetBool("IsAttackingLeft", true);
    }
    else if(currentFacingDirection == Vector2.right)
    {
        animator.SetBool("IsAttackingRight", true);
    }
    else if(currentFacingDirection == Vector2.down)
    {
        animator.SetBool("IsAttackingDown", true);
    }
    else if(currentFacingDirection == Vector2.up)
    {
        animator.SetBool("IsAttackingUp", true);
    }

    // 启动冷却
    canAttack = false;
    Invoke(nameof(EnableAttack), attackCooldown);
}

// 重置所有攻击布尔值
private void ResetAttackStates()
{
    animator.SetBool("IsAttackingLeft", false);
    animator.SetBool("IsAttackingRight", false);
    animator.SetBool("IsAttackingDown", false);
    animator.SetBool("IsAttackingUp", false);
}

// 冷却结束后允许再次攻击
private void EnableAttack()
{
    canAttack = true;
}

4. 动画事件绑定

在每个攻击动画的最后一帧添加动画事件,选择脚本中的ResetAttackStates函数,这样动画播放完毕后会自动重置攻击状态,不会一直停留在攻击状态。

三、额外优化建议

  • 若需攻击时禁止移动,可以添加private bool isAttacking变量,在FixedUpdate的移动逻辑前判断if(isAttacking) return;,攻击触发时设为true,ResetAttackStates中设为false。
  • 调整attackCooldown数值匹配攻击动画长度,避免动画未播放完就触发下一次攻击。
  • 确保Animator过渡设置中,攻击状态的优先级高于移动状态,避免动画冲突。

内容的提问来源于stack exchange,提问作者Wizard Gamireal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 09:05:28