Unity类银河恶魔城学习记录4-5 P59 EnemyStunnedState源代码

本文主要是介绍Unity类银河恶魔城学习记录4-5 P59 EnemyStunnedState源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

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

EntityFX.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EntityFX : MonoBehaviour
{private SpriteRenderer sr;//定义SR组件来保持要用的组件[Header("Flash FX")][SerializeField] private Material hitMat;//要改成的材料[SerializeField] private float flashDuration;//闪光的时间private Material originalMat;//原来的材料private void Start(){sr = GetComponentInChildren<SpriteRenderer>();//从子组件中拿到SR组件originalMat = sr.material;//拿到原来的材料}private IEnumerator FlashFX()//被打后该触发的函数{sr.material = hitMat;yield return new WaitForSeconds(flashDuration);sr.material = originalMat;} //IEnumertor本质就是将一个函数分块执行,只有满足某些条件才能执行下一段代码,此函数有StartCoroutine调用//https://www.zhihu.com/tardis/bd/art/504607545?source_id=1001private void RedColorBlink()//使角色闪烁的函数{if (sr.color != Color.white){sr.color = Color.white;}else{sr.color = Color.red;}}private void CancelRedBlink()//使角色停止闪烁的函数{CancelInvoke();//取消该 MonoBehaviour 上的所有 Invoke 调用。//https://docs.unity3d.com/cn/current/ScriptReference/MonoBehaviour.CancelInvoke.htmlsr.color = Color.white;}
}
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class Enemy : Entity
{[Header("Stun Info")]public float stunnedDuration;//stunned持续时间public Vector2 stunnedDirection;//stunned改变后的速度[SerializeField] protected LayerMask whatIsPlayer;[Header("Move Info")]public float moveSpeed;public float idleTime;public float battleTime;//多久能从battle状态中退出来[Header("Attack Info")]public float attackDistance;public float attackCooldown;//攻击冷却[HideInInspector]public float lastTimeAttacked;//最后一次攻击的时间#region 类public EnemyStateMachine stateMachine;#endregionprotected override void Awake(){base.Awake();stateMachine = new EnemyStateMachine();}protected override void Start(){base.Start();}protected override void Update(){base.Update();stateMachine.currentState.Update();//Debug.Log(IsPlayerDetected().collider.gameObject.name + "I see");//这串代码会报错,可能使版本的物体,因为在没有找到Player的时候物体是空的,NULL,你想让他在控制台上显示就报错了}public virtual void AnimationFinishTrigger() => stateMachine.currentState.AnimationFinishTrigger();//动画完成时调用的函数,与Player相同public virtual RaycastHit2D IsPlayerDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, 7, whatIsPlayer);//用于从射线投射获取信息的结构。//该函数的返回值可以变,可以只返回bool,也可以是碰到的结构protected override void OnDrawGizmos(){base.OnDrawGizmos();Gizmos.color = Color.yellow;//把线改成黄色Gizmos.DrawLine(transform.position, new Vector3(transform.position.x + attackDistance * facingDir, transform.position.y));//用来判别是否进入attackState的线}}

Enemy_Skeleton.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditorInternal;
using UnityEngine;public class Enemy_Skeleton : Enemy
{#region 类Statepublic SkeletonIdleState idleState { get; private set; }public SkeletonMoveState moveState { get; private set; }public SkeletonBattleState battleState { get; private set; }public SkeletonAttackState attackState { get; private set; }public SkeletonStunnedState stunnedState { get; private set; }#endregionprotected override void Awake(){base.Awake();idleState = new SkeletonIdleState(this, stateMachine, "Idle", this);moveState = new SkeletonMoveState(this,stateMachine, "Move", this);battleState = new SkeletonBattleState(this, stateMachine, "Move", this);attackState = new SkeletonAttackState(this, stateMachine, "Attack", this);stunnedState = new SkeletonStunnedState(this, stateMachine, "Stunned", this);}protected override void Start(){base.Start();stateMachine.Initialize(idleState);}protected override void Update(){base.Update();if(Input.GetKeyDown(KeyCode.U)){stateMachine.ChangeState(stunnedState);//暂时用U来控制进入stunned}}
}
SkeletonStunnedState.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SkeletonStunnedState : EnemyState
{private Enemy_Skeleton enemy;public SkeletonStunnedState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Skeleton _enemy) : base(_enemyBase, _stateMachine, _animBoolName){enemy = _enemy;}public override void Enter(){base.Enter();enemy.fx.InvokeRepeating("RedColorBlink", 0, .1f);//在 time 秒后调用 methodName 方法,然后每 repeatRate 秒调用一次。//https://docs.unity3d.com/cn/current/ScriptReference/MonoBehaviour.InvokeRepeating.htmlstateTimer = enemy.stunnedDuration;//stunned持续时间rb.velocity = new Vector2(-enemy.facingDir*enemy.stunnedDirection.x, enemy.stunnedDirection.y);//stunned改变后的速度,由于SetVelocity有FlipCheck,所有这个用rb.velocity设置速度}public override void Exit(){base.Exit();enemy.fx.Invoke("CancelRedBlink",0); //在 time 秒后调用 methodName 方法。//https://docs.unity3d.com/cn/current/ScriptReference/MonoBehaviour.Invoke.html}public override void Update(){base.Update();if(stateTimer < 0){stateMachine.ChangeState(enemy.idleState);}}
}

这篇关于Unity类银河恶魔城学习记录4-5 P59 EnemyStunnedState源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/689565

相关文章

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

java对接海康摄像头的完整步骤记录

《java对接海康摄像头的完整步骤记录》在Java中调用海康威视摄像头通常需要使用海康威视提供的SDK,下面这篇文章主要给大家介绍了关于java对接海康摄像头的完整步骤,文中通过代码介绍的非常详细,需... 目录一、开发环境准备二、实现Java调用设备接口(一)加载动态链接库(二)结构体、接口重定义1.类型

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、