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

Unity武器后坐动画Bool值立即变为false问题求助

问题解决思路

你的核心问题出在Update方法里的逻辑:每帧都会检查WeaponFire布尔值,只要是true就立刻设为false。这就导致Fire()里刚把值设为true,下一帧就被改回去,动画根本没时间播放。而删掉这段代码后,布尔值一直保持true,动画自然会循环播放。

下面给你几种可行的解决办法:

方法一:用动画事件(最推荐)

  1. 打开你的后坐力动画剪辑,在动画的最后一帧添加一个动画事件。
  2. 在WeaponFire脚本里新增一个方法:
public void ResetFireBool()
{
    FPS_char.GetComponent<Animator>().SetBool("WeaponFire", false);
}
  1. 把动画事件的回调设置为这个ResetFireBool方法。

这样动画播放完的瞬间才会重置布尔值,既不会提前中断动画,也不会让动画循环。

方法二:用协程延迟重置

修改Fire()方法,添加协程来延迟设置布尔值为false:

public void Fire()
{
    Animator anim = FPS_char.GetComponent<Animator>();
    anim.SetBool("WeaponFire", true);
    // 启动协程,等待动画时长后重置布尔值
    StartCoroutine(ResetFireAfterAnimation(anim));

    RaycastHit hit;
    if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
    {
        if (rounds <= 0)
        {
            return;
        }
        rounds -= 1f;
        gameObject.GetComponent<AudioSource>().Play();
        EnemyManager enemyManager = hit.transform.GetComponent<EnemyManager>();
        if (enemyManager != null)
        {
            enemyManager.Hit(damage);
        }
    }
}

private IEnumerator ResetFireAfterAnimation(Animator anim)
{
    // 替换成你的后坐动画的实际时长,比如0.2秒
    yield return new WaitForSeconds(0.2f);
    anim.SetBool("WeaponFire", false);
}

注意要把等待时间改成你实际动画的时长,确保动画刚好播放完就重置布尔值。

方法三:检测动画状态再重置

修改Update方法,只在动画播放完毕后才重置布尔值:

void Update()
{
    Animator anim = FPS_char.GetComponent<Animator>();
    if (anim.GetBool("WeaponFire"))
    {
        // 检查动画是否播放完毕(替换成你的后坐动画状态名,比如"Recoil")
        AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("Recoil") && stateInfo.normalizedTime >= 1.0f)
        {
            anim.SetBool("WeaponFire", false);
        }
    }
}

这里要把"Recoil"改成你后坐力动画的状态名称,确保能准确识别动画是否结束。

最后记得删掉原来Update里那行一检测到true就设为false的代码,避免和新逻辑冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:15:41