连击非第一击无伤害

在连击攻击敌人时,每次攻击开始和结束都设置同一个变量用于标识是否处于攻击状态。我的标识逻辑基于动画事件,在动画开始的第一帧和结束的最后一帧设置变量。先将触发事件的代码挂载在对应的动画上。

cs 复制代码
using UnityEngine;

public class AttackStateBehaviour : StateMachineBehaviour
{

    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.GetComponent<PlayerAction>()?.OnAttackEnter();
        Debug.Log($"[正确进入] 攻击状态,时间:{Time.time}");
    }

    public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.GetComponent<PlayerAction>()?.OnAttackExit();
        Debug.Log($"[正确退出] 攻击状态,时间:{Time.time}");
    }
}

最初我的过渡动画时间为0.14s,测试结果为

这里发现不是我预期的开始->退出->开始->退出->开始->退出,第一次退出与第二次开始,第二次退出与第三次开始之间相隔时间都接近0.14s,现在将一二段攻击间缩短过渡时间到0.07s。

发现第一次退出和第二次开始之间相隔时间大约0.07s,第二次退出与第三次开始之间相隔大约0.14s。

这与我得预期相同,状态开始与结束受到过渡时间的影响,下面我将过渡时间设置为0,应该就能解决只有第一段攻击有伤害的问题。

符合预期,经检验,伤害判定也正常了,每段攻击都可以正确出伤。但是过渡时间设置为0导致了我的动画切换僵硬,我们需要使用其他办法。

可以直接在FSM有限状态机中,每个状态的进入与退出处设置,而不是用动画事件。现在我们加上过渡时间测试

符合预期,经检验,攻击判定也符合预期。其中一个攻击状态代码:

cs 复制代码
public void OnEnter()
{
    rootMotionController.applyRootMotion = true;
    rootMotionController.applyRootMotionPositionXZ = true;
    rootMotionController.applyRootMotionPositionY = false;
    rootMotionController.applyRootMotionRotation = true;
    rootMotionController.SetManualRotation(Quaternion.identity); // 清除程序旋转
    rootMotionController.SetManualMovement(Vector3.zero);

    playerBlackBoards.anim.SetTrigger("AttackTrigger");

    playerBlackBoards.anim.GetComponent<PlayerAction>()?.OnAttackEnter();
    Debug.Log($"[正确进入] 攻击状态,时间:{Time.time}");
}

public void OnExit()
{
    rootMotionController.applyRootMotion = false;

    playerBlackBoards.anim.GetComponent<PlayerAction>()?.OnAttackExit();
    Debug.Log($"[正确退出] 攻击状态,时间:{Time.time}");
}
相关推荐
AtoposのCX3301 小时前
Docker运行hello-world镜像失败或超时
运维·docker
熊延2 小时前
麒麟V10系统安装部署elasticsearch
linux·运维·服务器·elasticsearch·搜索引擎·全文检索
larance5 小时前
Gunicorn + Nginx+systemd 配置flask
nginx·flask·gunicorn
Yeats_Liao5 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
爱吃生蚝的于勒6 小时前
【Linux】进程信号之捕捉(三)
linux·运维·服务器·c语言·数据结构·c++·学习
文艺理科生Owen6 小时前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
运维·nginx
期待のcode6 小时前
Redis的主从复制与集群
运维·服务器·redis
wangjialelele8 小时前
Linux下的IO操作以及ext系列文件系统
linux·运维·服务器·c语言·c++·个人开发
HypoxiaDream8 小时前
LINUX-Ext系列⽂件系统
linux·运维·服务器
小毛驴8508 小时前
Linux curl 命令用法
linux·运维·chrome