类银河恶魔城学习记录-Crash Course

2024-05-03 02:20

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

类银河恶魔城学习记录-Crash Course

本文记录一下学习过程中照着教程敲的代码~

Enemy_Skeleton.cs

using UnityEngine;
//骷髅敌人
public class Enemy_Skeleton : Entity
{[Header("Move Info")][SerializeField] private float moveSpeed;[Header("Player Detection")][SerializeField] private float playerCheckDistance;[SerializeField] private LayerMask whatIsPlayer;private RaycastHit2D isPlayerDetected;bool isAttacking;protected override void Start(){base.Start();}protected override void Update(){base.Update();//只要检测到玩家if (isPlayerDetected){if (isPlayerDetected.distance > 3){rb.velocity = new Vector2(facingDir * moveSpeed * 1.5f, rb.velocity.y);Debug.Log("I see the Player!");isAttacking = false;}else{Debug.Log("Attaking " + isPlayerDetected.collider.gameObject.name);isAttacking = true;}}if (!isGrounded || isWallDetected)Flip();Movement();}private void Movement(){if(!isAttacking)rb.velocity = new Vector2(facingDir * moveSpeed, rb.velocity.y);}protected override void CheckCollision(){base.CheckCollision();isPlayerDetected = Physics2D.Raycast(transform.position, Vector2.right, playerCheckDistance * facingDir, whatIsPlayer);}protected override void OnDrawGizmos(){base.OnDrawGizmos();Gizmos.color = Color.blue;Gizmos.DrawLine(transform.position, new Vector3(transform.position.x + playerCheckDistance * facingDir, transform.position.y));}
}

Entity.cs

using UnityEngine;
//实体类
public class Entity : MonoBehaviour
{protected Rigidbody2D rb;protected Animator anim;protected bool isGrounded;protected bool isWallDetected;[Header("Collision Info")][SerializeField] protected LayerMask whatIsGround;[SerializeField] protected float groundCheckDistance;[SerializeField] protected Transform groundCheck;[Space][SerializeField] protected Transform wallCheck;[SerializeField] protected float wallCheckDistance;protected int facingDir = 1;protected bool facingRight = true;protected virtual void Start(){rb = GetComponent<Rigidbody2D>();anim = GetComponentInChildren<Animator>();}protected virtual void Update(){CheckCollision();}//检测是否撞墙或者离开地面protected virtual void CheckCollision(){isGrounded = Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);isWallDetected = Physics2D.Raycast(wallCheck.position, Vector2.right, wallCheckDistance * facingDir, whatIsGround);}//转向protected virtual void Flip(){facingDir *= -1;facingRight = !facingRight;transform.Rotate(0, 180, 0);}//画线 检测撞墙的线和检测离地的线protected virtual void OnDrawGizmos(){Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance * facingDir, wallCheck.position.y));}
}

Player.cs

using UnityEngine;
//玩家类
public class Player : Entity
{[Header("Move Info")][SerializeField] private float xInput;[SerializeField] private float moveSpeed = 4;[SerializeField] private float junpForce = 7;[Header("Dash Info")][SerializeField] private float dashDuration;[SerializeField] private float dashTime;[SerializeField] private float dashSpeed;[SerializeField] private float dashCalmDuration;[SerializeField] private float dashCalmTime;[Header("Attack Info")][SerializeField] private bool isAttacking;[SerializeField] private int comboCounter;[SerializeField] private float comboDuration;[SerializeField] private float comboTimeWindow;protected override void Start(){base.Start();}protected override void Update(){base.Update();FlipController();Movement();CheckInput();AnimatorController();Dash();Attack();comboTimeWindow -= Time.deltaTime;}//鼠标左键攻击private void Attack(){if (!isGrounded)return;if (Input.GetKeyDown(KeyCode.Mouse0)){StartAttackEvent();}}//攻击事件private void StartAttackEvent(){if (comboTimeWindow < 0)comboCounter = 0;isAttacking = true;comboTimeWindow = comboDuration;}//攻击结束public void AttackOver(){isAttacking = false;comboCounter++;if (comboCounter > 2)comboCounter = 0;}//冲刺控制private void Dash(){dashTime -= Time.deltaTime;dashCalmTime -= Time.deltaTime;if (Input.GetKeyDown(KeyCode.LeftShift)){DashAbillity();}}//冲刺判定private void DashAbillity(){if (dashCalmTime < 0 && !isAttacking){dashTime = dashDuration;dashCalmTime = dashCalmDuration;}}//检查输入private void CheckInput(){xInput = Input.GetAxisRaw("Horizontal");if (Input.GetKeyDown(KeyCode.Space)){Jump();}}//移动控制private void Movement(){if (isAttacking){rb.velocity = new Vector2(0, 0);}else if(dashTime > 0){rb.velocity = new Vector2(facingDir * dashSpeed,0);}else{rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);}}//跳跃控制private void Jump(){if(isGrounded)rb.velocity = new Vector2(rb.velocity.x, junpForce);}//动画播放控制private void AnimatorController(){bool isMoving = rb.velocity.x != 0;anim.SetBool("isMoving", isMoving);anim.SetBool("isGrounded", isGrounded);anim.SetFloat("yVelocity", rb.velocity.y);anim.SetBool("isDashing", dashTime > 0);anim.SetBool("isAttacking", isAttacking);anim.SetInteger("comboCounter", comboCounter);}//转向控制private void FlipController(){if(rb.velocity.x > 0 && !facingRight)Flip();else if(rb.velocity.x < 0 && facingRight)Flip();}}

PlayerAnimEvents.cs

using UnityEngine;public class PlayerAnimEvents : MonoBehaviour
{private Player player;private void Start(){player = GetComponentInParent<Player>();}private void AnimationTrigger(){player.AttackOver();}
}

这篇关于类银河恶魔城学习记录-Crash Course的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

在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、