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 分钟前
使用LabelImg工具标注数据(游戏辅助脚本开发)
python·游戏引擎·图形渲染·骨骼绑定
崇山峻岭之间3 分钟前
C++ Prime Plus 学习笔记041
c++·笔记·学习
万岳科技系统开发16 分钟前
私域直播小程序源码的整体架构设计与实现思路
学习·小程序
richxu2025100127 分钟前
嵌入式学习之路>单片机核心原理篇>(11) 存储器(Flash & SRam)
单片机·嵌入式硬件·学习
sszdlbw38 分钟前
后端springboot框架入门学习--第二篇
java·spring boot·学习
AT~1 小时前
unity 使用Socket和protobuf实现网络连接
unity·游戏引擎
wearegogog1231 小时前
C# 条码打印程序(一维码 + 二维码)
java·开发语言·c#
9527(●—●)1 小时前
windows系统python开发pip命令使用(菜鸟学习)
开发语言·windows·python·学习·pip
sali-tec1 小时前
C# 基于halcon的视觉工作流-章69 深度学习-异常值检测
开发语言·图像处理·算法·计算机视觉·c#
我是唐青枫1 小时前
深入理解 C#.NET 运算符重载:语法、设计原则与最佳实践
开发语言·c#·.net