Unity类银河恶魔城学习记录1-14 AttackDirection源代码 P41

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

PlayerPrimaryAttackState.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPrimaryAttackState : PlayerState
{
    //p38 2.从ground进入

    private int comboCounter;

    private float lastTimeAttacked;//距离上一次攻击的时间
    private float comboWindow = 2;//可以间隔的时间
    public PlayerPrimaryAttackState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        if(comboCounter >2||Time.time>comboWindow+lastTimeAttacked)//当计数器超过2和间隔时间大于window时,进入第一个攻击动作
        {
            comboCounter = 0;
        }
        Debug.Log(comboCounter);

        player.anim.SetInteger("ComboCounter", comboCounter);//设置animtor里的comboCounter

        #region 选择攻击方向
        float attackDir = player.facingDir;

        if(xInput != 0)
        {
            attackDir = xInput;
        }
        #endregion
                                                                   //使其能改变攻击方向
        player.SetVelocity(player.attackMovement[comboCounter].x * attackDir, player.attackMovement[comboCounter].y);//给角色初速度,让角色在攻击触发时移动一点

        stateTimer = .1f;
    }

    public override void Exit()
    {
        base.Exit();
        player.StartCoroutine("BusyFor", .15f);
        comboCounter++;
        lastTimeAttacked = Time.time;
    }

    public override void Update()
    {
        base.Update();
        if(stateTimer<0)
        {
            player.ZeroVelocity();
        }//1.修改移动时攻击时后可以移动的BUG
        //2.但给了点时间模拟惯性可以动一点
        if (triggerCalled)
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}
相关推荐
数字芯片实验室2 小时前
分享一个可以学习正则表达式的网址:Pythex.org
学习·正则表达式
陈洪奇2 小时前
注册中心学习笔记整理
笔记·学习
光影少年2 小时前
从前端转go开发的学习路线
前端·学习·golang
我是苏苏4 小时前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#
fen_fen8 小时前
学习笔记(32):matplotlib绘制简单图表-数据分布图
笔记·学习·matplotlib
AA陈超10 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
水果里面有苹果11 小时前
20-C#构造函数--虚方法
java·前端·c#
萝卜青今天也要开心12 小时前
2025年上半年软件设计师考后分享
笔记·学习
amazinging12 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十七天
python·学习·selenium
吃货界的硬件攻城狮12 小时前
【STM32 学习笔记】SPI通信协议
笔记·stm32·学习