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

相关文章

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、配置重试模板

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat