Unity类银河恶魔城学习记录3-2 Entity源代码 P48

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

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

 Entity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Entity : MonoBehaviour
{[Header("Collision Info")][SerializeField] protected Transform groundCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] protected float groundCheckDistance;[SerializeField] protected Transform wallCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] protected float wallCheckDistance;[SerializeField] protected LayerMask whatIsGround;//LayerMask类,与Raycast配合,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html#region 定义Unity组件public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权public Rigidbody2D rb { get; private set; }//配合拿到身上的Rigidbody2D组件控制权#endregionpublic int facingDir { get; private set; } = 1;protected bool facingRight = true;//判断是否朝右protected virtual void Awake(){anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权rb = GetComponent<Rigidbody2D>();}protected virtual void Start(){}protected virtual void Update(){}#region 速度函数Velocitypublic virtual void ZeroVelocity(){rb.velocity = new Vector2(0, 0);}//设置速度为0函数public virtual void SetVelocity(float _xVelocity, float _yVelocity){rb.velocity = new Vector2(_xVelocity, _yVelocity);//将rb的velocity属性设置为对应的想要的二维向量。因为2D游戏的速度就是二维向量FlipController(_xVelocity);//在其他设置速度的时候调用翻转控制器}//控制速度的函数,此函数在其他State中可能会使用,但仅能通过player.SeVelocity调用#endregion#region 翻转函数Flippublic virtual void Flip(){facingDir = facingDir * -1;facingRight = !facingRight;transform.Rotate(0, 180, 0);//旋转函数,transform不需要额外定义,因为他是自带的}//翻转函数public virtual void FlipController(float _x)//目前设置x,目的时能在空中时也能转身{if (_x > 0 && !facingRight)//当速度大于0且没有朝右时,翻转{Flip();}else if (_x < 0 && facingRight){Flip();}}#endregion#region 碰撞函数Collisionpublic virtual bool IsGroundDetected(){return Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);}//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html//xxxxxxxx()   => xxxxxxxx  == xxxxxxxxxx() return xxxxxxxxx;public virtual bool IsWallDetected(){return Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround);}//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html//xxxxxxxx()   => xxxxxxxx  == xxxxxxxxxx() return xxxxxxxxx;protected virtual void OnDrawGizmos(){Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。}//画线函数#endregion
}
 Player.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class Player : Entity
{[Header("Attack Details")]public Vector2[] attackMovement;//每个攻击时获得的速度组public bool isBusy{ get; private set; }//防止在攻击间隔中进入move//[Header("Move Info")]public float moveSpeed;//定义速度,与xInput相乘控制速度的大小public float jumpForce;[Header("Dash Info")][SerializeField] private float dashCooldown;private float dashUsageTimer;//为dash设置冷却时间,在一定时间内不能连续使用public float dashSpeed;//冲刺速度public float dashDuration;//持续时间public float dashDir { get; private set; }#region 定义Statespublic PlayerStateMachine stateMachine { get; private set; }public PlayerIdleState idleState { get; private set; }public PlayerMoveState moveState { get; private set; }public PlayerJumpState jumpState { get; private set; }public PlayerAirState airState { get; private set; }public PlayerDashState dashState { get; private set; }public PlayerWallSlideState wallSlide { get; private set; }public PlayerWallJumpState wallJump { get; private set; }public PlayerPrimaryAttackState primaryAttack { get; private set; }#endregionprotected override void Awake(){base.Awake();stateMachine = new PlayerStateMachine();//通过构造函数,在构造时传递信息idleState = new PlayerIdleState(this, stateMachine, "Idle");moveState = new PlayerMoveState(this, stateMachine, "Move");jumpState = new PlayerJumpState(this, stateMachine, "Jump");airState = new PlayerAirState(this, stateMachine, "Jump");dashState = new PlayerDashState(this, stateMachine, "Dash");wallSlide = new PlayerWallSlideState(this, stateMachine, "WallSlide");wallJump = new PlayerWallJumpState(this, stateMachine, "Jump");//wallJump也是Jump动画primaryAttack = new PlayerPrimaryAttackState(this, stateMachine, "Attack");//this 就是 Player这个类本身}//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)protected override void Start(){base.Start();stateMachine.Initialize(idleState);}protected override void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update{base.Update();stateMachine.currentState.Update();//反复调用CurrentState的Update函数CheckForDashInput();}public IEnumerator BusyFor(float _seconds)//https://www.zhihu.com/tardis/bd/art/504607545?source_id=1001{isBusy = true;yield return new WaitForSeconds(_seconds);isBusy = false;}//p39 4.防止在攻击间隔中进入move,通过设置busy值,在使用某些状态时,使其为busy为true,抑制其进入其他state//IEnumertor本质就是将一个函数分块执行,只有满足某些条件才能执行下一段代码,此函数有StartCoroutine调用public void AnimationTrigger() => stateMachine.currentState.AnimationFinishTrigger();//从当前状态拿到AnimationTrigger进行调用的函数public void CheckForDashInput(){dashUsageTimer -= Time.deltaTime;//给dash上冷却时间if (IsWallDetected()){return;}//修复在wallslide可以dash的BUGif (Input.GetKeyDown(KeyCode.LeftShift) && dashUsageTimer < 0){dashUsageTimer = dashCooldown;dashDir = Input.GetAxisRaw("Horizontal");//设置一个值,可以将dash的方向改为你想要的方向而不是你的朝向if (dashDir == 0){dashDir = facingDir;//只有当玩家没有控制方向时才使用默认朝向}stateMachine.ChangeState(dashState);}}//将Dash切换设置成一个函数,使其在所以情况下都能使用}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/670512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2.6 作业

第五章 静态成员与友元 一、填空题 1、一个类的头文件如下所示&#xff0c;num初始化值为5&#xff0c;程序产生对象T&#xff0c;且修改num为10&#xff0c;并使用show()函数输出num的值10。 #include <iostream.h> class Test { private: static int num; public: Tes…

Android13 系统源码适配安装可卸载的三方apk应用

Android13 系统源码适配安装可卸载的三方apk应用 文章目录 Android13 系统源码适配安装可卸载的三方apk应用一、前言二、Android 系统运行后默认安装三方apk实现1、Android 系统默认安装三方apk实现主要思路2、Android 系统默认安装三方apk具体实现&#xff08;1&#xff09;准…

chartGPT网站

在人工智能日益发展的今天&#xff0c;大语言模型成为了我们生活与工作中不可或缺的一部分。它们能够帮助我们处理文本、回答问题、生成创意内容&#xff0c;甚至协助编程和数据分析。然而&#xff0c;由于网络环境的复杂性&#xff0c;找到一个既稳定又快速的大语言模型网站并…

C++ 中template 模版

C模板&#xff08;Templates&#xff09;主要是编写泛型代码&#xff0c;在编译时为不同数据类型生成相应的函数或类实例&#xff0c;主要有函数模板&#xff0c;类模板 函数模板 #include <iostream> #include <string>using namespace std;template<typenam…

OLAP技术的发展及趋势简述

这里写自定义目录标题 历史发展基于电子表格的数据分析基于传统数据库的数据分析基于大数据的数据分析 当下的现状OLAP技术的分类MOLAPROLAPHOLAP 主流的OLAP引擎新技术的普及内存向量计算列式数据存储及交换增量查询多源融合计算下推物化视图 发展趋势智能化分析多源融合和自动…

C Primer Plus(第六版)15.9 编程练习 第3题

#include <stdio.h> void getbit(int num); int main() { getbit(127); return 0; } void getbit(int num) { int result; int sum0; int mask1; result num; while(result!0) { if((result & mask) 1) sum; result >> 1; } printf("%d打开的位有%d个…

【学习笔记】树上差分总结(点差分/边差分)

一.树上差分的基本概念 1.树上差分的定义 树上差分&#xff0c;顾名思义&#xff0c;意思就是在树上做差分。 至于什么是差分呢&#xff1f;如果不会的同学&#xff0c;可以先看看我的这篇博客:一维,二维差分の详解&#xff08;简单易懂&#xff09;_一维差分-CSDN博客 2.树…

智能小程序框架——模块化语法参考(ESModule 语法、CommonJS 语法)

小程序的逻辑代码使用 JavaScript 语法进行编写。可以将一些公共的代码抽离成为一个单独的 js 文件&#xff0c;作为一个模块&#xff0c;以实现逻辑共享复用。主要有 ESModule 和 CommonJS 两种形式。一个项目内应保持使用一种语法形式&#xff0c;否则可能出现导入异常的情况…

SpringBoot中使用Spring自带线程池ThreadPoolTaskExecutor与Java8CompletableFuture实现异步任务示例

场景 关于线程池的使用&#xff1a; Java中ExecutorService线程池的使用(Runnable和Callable多线程实现)&#xff1a; Java中ExecutorService线程池的使用(Runnable和Callable多线程实现)_executorservice executorservice executors.newfix-CSDN博客 Java中创建线程的方式…

springboot(ssm考试信息报名系统 在线考试报名系统Java系统

springboot(ssm考试信息报名系统 在线考试报名系统Java系统 开发语言&#xff1a;Java 框架&#xff1a;springboot&#xff08;可改ssm&#xff09; vue JDK版本&#xff1a;JDK1.8&#xff08;或11&#xff09; 服务器&#xff1a;tomcat 数据库&#xff1a;mysql 5.7&a…

React+echarts实现排名+自动滚动+X轴自定义titleTop

1、效果 2、环境准备 1、react18 2、antd 4 3、代码实现 原理&#xff1a;自动滚动通过创建定时器动态更新echar的dataZoom属性startValue、endValue&#xff0c;自定义tooltip通过监听echar的鼠标移入移出事件&#xff0c;判断tooltTip元素的显隐以及位置。 /*** 收集完成…

【Java EE】----Spring框架创建和使用

1.Spring框架创建 创建一个maven项目 添加Spring框架支持 <dependencies> 上下文<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.3.RELEASE</version></depende…

【网站项目】038汽车养护管理系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

java -jar启动SpringBoot项目时配置文件加载位置与优先级

服务部署启动时,我们经常需要指定配置文件启动. 一般有四种,优先级如下 spring.config.location > spring.profiles.active > spring.config.additional-location > 默认的 application.yml 1.spring.config.location 外部配置文件优先级最高 一般配置文件在服务…

PS之外的平面设计利器:7款高效软件帮你轻松搞定

平面图设计的痕迹体现在日常生活的方方面面&#xff0c;如路边传单、杂志、产品包装袋或手机开屏海报等&#xff0c;平面设计软件层出不穷。Photoshop是大多数平面图设计初学者的入门软件&#xff0c;但随着设计师需求的不断提高&#xff0c;平面图设计软件Photoshop逐渐显示出…

SpringBoot:自动配置报告

自动配置报告demo&#xff1a;点击查看 LearnSpringBoot03AutoConfig 点击查看更多的SpringBoot教程 一、application.properties代码 #开启springboot debug模式 #自动配置报告 #Positive matches: 自动配置类启用了&#xff08;自动配置类匹配上了&#xff09;&#xff0c…

【动态规划】【图论】【C++算法】1928规定时间内到达终点的最小花费

作者推荐 【动态规划】【状态压缩】【2次选择】【广度搜索】1494. 并行课程 II 本文涉及知识点 动态规划汇总 LeetCode1928. 规定时间内到达终点的最小花费 一个国家有 n 个城市&#xff0c;城市编号为 0 到 n - 1 &#xff0c;题目保证 所有城市 都由双向道路 连接在一起…

【python】绘制春节烟花

一、Pygame库春节烟花示例 下面是一个使用Pygame实现的简单春节烟花效果的示例代码。请注意&#xff0c;运行下面的代码之前&#xff0c;请确保计算机上已经安装了Pygame库。 import pygame import random import math from pygame.locals import *# 初始化pygame pygame.ini…

Spring Boot配置文件优先级

1、bat文件启动java程序 java -Dmmmqqq -Dfile.encodingUTF-8 -jar ruoyi-admin.jar --mmmiii --llllll 2、配置类型 程序参数Program arguments : --mmmiii 单个属性值&#xff0c;可以从String[] args读取到&#xff0c;放在jar包命令后面 VM参数VM options :一般以-D …

如何让 Pages 文字分为两栏或更多栏?

通常一份文件都是由上往下仅有「一栏」而已&#xff0c;但在某些情况的排版&#xff0c;我们需要两栏甚至三栏的设计&#xff0c;在Pages 要如何做到呢&#xff1f;来看看吧。 将 Pages 文件改为双栏式设计 点一下「格式」>「布局」&#xff0c;就可以看到预设的「直栏」数…