类银河恶魔城学习记录-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

相关文章

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Spring Boot中定时任务Cron表达式的终极指南最佳实践记录

《SpringBoot中定时任务Cron表达式的终极指南最佳实践记录》本文详细介绍了SpringBoot中定时任务的实现方法,特别是Cron表达式的使用技巧和高级用法,从基础语法到复杂场景,从快速启... 目录一、Cron表达式基础1.1 Cron表达式结构1.2 核心语法规则二、Spring Boot中定

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)

《国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)》本文给大家利用deepseek模型搭建私有知识问答库的详细步骤和遇到的问题及解决办法,感兴趣的朋友一起看看吧... 目录1. 第1步大家在安装完ollama后,需要到系统环境变量中添加两个变量2. 第3步 “在cmd中

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板