当前位置: 首页 > news >正文

类银河恶魔城学习记录1-6 Flip基本设置源代码 P33

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

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

Player.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class Player : MonoBehaviour
{[Header("Move Info")]public float moveSpeed;//定义速度,与xInput相乘控制速度的大小public float jumpForce;[Header("Collision Info")][SerializeField] private Transform groundCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] private float groundCheckDistance;[SerializeField] private Transform wallCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] private float wallCheckDistance;[SerializeField] private LayerMask whatIsGround;//LayerMask类,与Raycast配合,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.htmlpublic int facingDir { get; private set; } = 1;//暂时没有用private bool facingRight = true;//判断是否朝右#region 定义Unity组件public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权public Rigidbody2D rb { get; private set; }//配合拿到身上的Rigidbody2D组件控制权#endregion#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; }#endregionprivate void 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");//this 就是 Player这个类本身}//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)private void Start(){anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权rb = GetComponent<Rigidbody2D>();stateMachine.Initialize(idleState);}private void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update{stateMachine.currentState.Update();//反复调用CurrentState的Update函数}public void SetVelocity(float _xVelocity,float _yVelocity){rb.velocity = new Vector2(_xVelocity, _yVelocity);//将rb的velocity属性设置为对应的想要的二维向量。因为2D游戏的速度就是二维向量FlipController(_xVelocity);//在其他设置速度的时候调用翻转控制器}//控制速度的函数,此函数在其他State中可能会使用,但仅能通过player.SeVelocity调用public 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;private 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(后面的) 的线。}//画线函数public void Flip(){facingDir = facingDir * -1;facingRight = !facingRight;transform.Rotate(0, 180, 0);//旋转函数,transform不需要额外定义,因为他是自带的}//翻转函数public void FlipController(float _x)//目前设置x,目的时能在空中时也能转身{if(_x > 0 &&!facingRight)//当速度大于0且没有朝右时,翻转{Flip();}else if(_x < 0 && facingRight){Flip();}}
}

PlayerState.cs
using System.Collections;
using System.Collections.Generic;
using System.Security.Authentication.ExtendedProtection;
using UnityEngine;
//被继承的总类,后面的所以state都需要继承它
//实际上Player并没有进入过这个状态,没有调用此脚本,所有的调用均属于此脚本的子脚本
public class PlayerState
{protected PlayerStateMachine stateMachine;//创建PlayerStateMachine类,以承接来自Player.cs传过来的东西protected Player player;//创建Player类,以承接来自Player.cs传过来的东西#region Unity组件protected Rigidbody2D rb;//纯简化,将player.rb --> rb#endregionprivate string animBoolName;//控制此时的anim的BOOL值protected float xInput;//设置一个可以保存当前是否在按移动键的变量public PlayerState(Player _player,PlayerStateMachine _stateMachine,string _animBoolName){this.player = _player;this.stateMachine = _stateMachine;                                                this.animBoolName = _animBoolName;}//构造函数,用于传递信息public virtual void Enter(){player.anim.SetBool(animBoolName, true);//通过animator自带的函数,将animator里的Bool值改变为想要的值rb = player.rb;//纯简化,将player.rb --> rb}public virtual void Update(){player.anim.SetFloat("yVelocity", rb.velocity.y);//通过animator自带的函数,将animator里的Float值改变为想要的值,改变yVelocity以控制Jump与Fall动画的切换xInput = Input.GetAxisRaw("Horizontal");//对于键盘和游戏杆输入,该值将处于 -1...1 的范围内。 由于未对输入进行平滑处理,键盘输入将始终为 -1、0 或 1。 如果您想自己完成键盘输入的所有平滑处理,这非常有用。}public virtual void Exit(){player.anim.SetBool(animBoolName, false);//通过animator自带的函数,将animator里的Bool值改变为想要的值}}

PlayerStateMachine.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerStateMachine
{public PlayerState currentState { get; private set; }public void Initialize(PlayerState _startState){currentState = _startState;currentState.Enter();}//初始化状态函数,及通过将idleState传送进来,以便于调用idleState中的Enter函数public void ChangeState(PlayerState _newState){currentState.Exit();currentState = _newState;currentState.Enter();}//更改状态函数//1.调用当前状态的Exit函数,使动画为false//2.传入新的状态,替换原来的状态//3.调用新的状态的Enter函数
}

PlayerGroundState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//GroundState用于保证只有在Idle和Move这两个地面状态下才能调用某些函数,并且稍微减少一点代码量
public class PlayerGroundState : PlayerState
{public PlayerGroundState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();}public override void Update(){base.Update();if (Input.GetKeyDown(KeyCode.Space)&&player.IsGroundDetected()){stateMachine.ChangeState(player.jumpState);}//空格切换为跳跃状态}public override void Exit(){base.Exit();}}

PlayerIdleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerIdleState : PlayerGroundState//继承函数,获得PlayerState里的所有函数和参数
{public PlayerIdleState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}//构造函数,用于传递信息。//当外补New出对象时,New出的对象里传入参数public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(xInput != 0){stateMachine.ChangeState(player.moveState);//在这里我们能使用Player里的东西,主要是因为我们通过构造函数传过来Player实体,如果不传,则无法使用已经存在了的Player实体。}}
}

PlayerMoveState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerMoveState : PlayerGroundState
{public PlayerMoveState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}//构造函数,用于传递信息。//当外补New出对象时,New出的对象里传入参数public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();player.SetVelocity(xInput*player.moveSpeed, rb.velocity.y);//player.rb.velocity.y默认的为0,player.moveSpeed在player中定义,但可以在Unity引擎中更改if (xInput==0){stateMachine.ChangeState(player.idleState);//在这里我们能使用Player里的东西,主要是因为我们通过构造函数传过来Player实体,如果不传,则无法使用已经存在了的Player实体。}}
}

PlayerJumpState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerJumpState : PlayerState
{public PlayerJumpState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();rb.velocity = new Vector2(rb.velocity.x, player.jumpForce);//将y轴速度改变}public override void Update(){base.Update();if (rb.velocity.y < 0)//当速度小于0时切换为airState{stateMachine.ChangeState(player.airState);//与其说是airState,不如说是FallState}}public override void Exit(){base.Exit();}}

PlayerAirState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerAirState : PlayerState
{public PlayerAirState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();}public override void Update(){base.Update();if (player.IsGroundDetected()){stateMachine.ChangeState(player.idleState);}//暂时使用,因为这样写是由错误的}public override void Exit(){base.Exit();}}

相关文章:

类银河恶魔城学习记录1-6 Flip基本设置源代码 P33

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释&#xff0c;可供学习Alex教程的人参考 【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili Player.cs using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; us…...

【Git教程】(一)基本概念 ——工作流、分布式版本控制、版本库 ~

Git教程 基本概念 1️⃣ 为什么要用 Git2️⃣ 为什么要用工作流3️⃣ 分布式版本控制4️⃣ 版本库5️⃣ 简单的分支创建与合并&#x1f33e; 总结 在本章中&#xff0c;将介绍一个分布式版本控制系统的设计思路&#xff0c;以及它与集中式版本控制系统的不同之处。除此之外&am…...

【QT】VS-code报错:LNK2019: 无法解析的外部符号

目录 0.环境 1.问题简述 2.分析报错原因 3.解决方法 1&#xff09;set() 相关语句 2&#xff09;target_link_libraries() 相关语句 4.参考 0.环境 windows11 、 vs-code 、 qt 、 c、编译器为vs2019-x86_amd64 1.问题简述 项目编译release版本时会报错&#xff1a;报错…...

基于 NXP S32K344 的汽车通用评估板方案

S32K3xx 系列是 NXP 基于 ARMCortex-M7 的汽车工业级 MCU&#xff0c;符合 ISO26262 ASIL-D 汽车功能安全等级&#xff0c;支持 ASIL B/D 安全应用&#xff0c;提供了一个可扩展的平台&#xff0c;具有下一代安全性、可扩展性、连接性和低功耗特性。适用于可能会在严酷环境下工…...

WebAssembly002 FFmpegWasmLocalServer项目

项目介绍 https://github.com/incubated-geek-cc/FFmpegWasmLocalServer.git可将音频或视频文件转换为其他可选的多媒体格式&#xff0c;并导出转码的结果 $ bash run.sh FFmpeg App is listening on port 3000!运行效果 相关依赖 Error: Cannot find module ‘express’ …...

瑞_力扣LeetCode_二叉树相关题

文章目录 说明题目 144. 二叉树的前序遍历题解 题目 94. 二叉树的中序遍历题解 题目 145. 二叉树的后序遍历题解 题目 105. 从前序与中序遍历序列构造二叉树题解 题目 106. 从中序与后序遍历序列构造二叉树题解 &#x1f64a; 前言&#xff1a;本文章为瑞_系列专栏之《刷题》的…...

Axios设置token到请求头的三种方式

1、为什么要携带token? 用户登录时&#xff0c;后端会返回一个token&#xff0c;并且保存到浏览器的localstorage中&#xff0c;可以根据localstorage中的token判断用户是否登录&#xff0c;登录后才有权限访问相关的页面&#xff0c;所以当发送请求时&#xff0c;都要携带to…...

微服务介绍、使用 Nacos 实现远程调用以及 OpenFeign 的使用

1 微服务的概念 区别于单体项目 单体项目拆分成微服务项目的目标&#xff1a;高内聚、低耦合 拆分思路 纵向拆分&#xff1a;根据功能模块 横向拆分&#xff1a;抽取可复用模块 2 微服务拆分——远程调用 背景&#xff1a;微服务单一职责&#xff0c;每个服务只有自己的功能…...

Arthas使用教程—— 阿里开源线上监控诊断产品

文章目录 1 简介2背景3 图形界面工具 arthas 阿里开源3.1 &#xff1a;启动 arthas3.2 help :查看arthas所有命令3.3 查看 dashboard3.4 thread 列出当前进程所有线程占用CPU和内存情况3.5 jvm 查看该进程的各项参数 &#xff08;类比 jinfo&#xff09;3.6 通过 jad 来反编译 …...

mac电脑快捷指令实现拼图

mac访达&#xff0c;搜索输入‘快捷指令’&#xff0c;找到‘快捷指令’&#xff0c; 点击快捷指令&#xff0c;进入快捷指令中心&#xff0c;搜索‘拼图’ &#xff0c;选中‘照片拼图’&#xff0c; 点击‘添加快捷指令’&#xff0c; 在‘所有快捷键指令’中可以看到添加的快…...

R语言入门笔记2.1

分支、循环与函数&#xff08;1&#xff09; 1.if语句 在R语言中&#xff0c;if语句用于根据条件执行不同的代码块。其基本语法如下&#xff1a; if (condition) {# 如果条件为真&#xff0c;执行这里的代码块 } else {# 如果条件为假&#xff0c;执行这里的代码块 } 其中&…...

补题:leetcode第382场周赛 3022. 给定操作次数内使剩余元素的或值最小

3022. 给定操作次数内使剩余元素的或值最小 - 力扣&#xff08;LeetCode&#xff09; 拆位 n个数进行或运算的结果最小&#xff0c;每次操作可以对相邻的两个数进行与运算&#xff0c;至多进行k次操作 n个数进行或运算&#xff0c;可以对每个数进行拆解&#xff0c;拆解成最小…...

创建型模式-单例模式:定义、实现及应用

目录 一、模式定义二、针对问题1.解决的问题2.解决方案3.举个例子4.设计模式适合场景5.实现方式6.优缺点7.与其他模式的关系 三、代码实现 一、模式定义 单例模式&#xff08;Singleton Pattern&#xff09;是一种创建型模式&#xff0c;用于限制某个类只能创建一个对象。它提…...

Prime(VulnHub)

Prime 文章目录 Prime1、nmap2、web渗透随便看看首页隐写查看目录爆破gobusterferoxbusterdirsearchdirb whatwebsearchsploit WordPress 5.2.2/dev/secret.txtFuzz_For_Webwfuzzimage.phpindex.php location.txtsecrettier360文件包含漏洞包含出password.txt尝试ssh登入尝试登…...

爬虫工作量由小到大的思维转变---<第四十二章 Scrapy Redis 重试机制(ip相关)>

前言: 之前讲过一篇关于scrapy的重试机制的文章,那个是针对当时那哥们的代码讲的,但是,发现后面还是有很多问题; 本章节就着scrapy的重试机制来讲一下!!! 正文: 首先,要清楚一个概念,在scrapy的中间件中,默认会有一个scrapy重试中间件;只要你在settings.py设置中写上: RETR…...

python日志管理配置

日志基础配置文件 日志回转查看&#xff1a;参考&#xff1a;https://blog.csdn.net/B11050729/article/details/132353220 项目使用注解实现 """ settings.py logging配置 """ import osroot_dir os.path.normpath(os.path.join(os.path.ab…...

2024.1.28力扣每日一题——水壶问题

2024.1.28 题目来源我的题解方法一 深度搜索&#xff08;DFS&#xff09;/广度搜索&#xff08;BFS&#xff09;方法二 数学 题目来源 力扣每日一题&#xff1b;题序&#xff1a;365 我的题解 方法一 深度搜索&#xff08;DFS&#xff09;/广度搜索&#xff08;BFS&#xff…...

orin nx 安装paddlespeech记录

nx配置&#xff1a; 模块 版本说明 CPU 8核 内存 16G Cuda版本 11.4 Opencv版本 4.5.4 Tensorrt版本 5.1 Cudnn版本 8.6.0.166 Deepstream版本 6.2 Python版本 3.8 算力 100T 安装paddlepaddle&#xff1a; 去飞桨官网下载jetpack版本的&#xff1a;下…...

系统架构设计师-21年-上午答案

系统架构设计师-21年-上午答案 更多软考资料 https://ruankao.blog.csdn.net/ 1 ~ 10 1 前趋图(Precedence Graph)是一个有向无环图&#xff0c;记为:→{(Pi,Pj)|Pi must complete before Pj may strat}&#xff0c;假设系统中进程P{P1&#xff0c;P2&#xff0c;P3&#xf…...

外包干了10个月,技术退步明显...

先说一下自己的情况&#xff0c;大专生&#xff0c;18年通过校招进入武汉某软件公司&#xff0c;干了接近4年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落! 而我已经在一个企业干了四年的功能测…...

Unity3D中Gfx.WaitForPresent优化方案

前言 在Unity中&#xff0c;Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染&#xff08;即CPU被阻塞&#xff09;&#xff0c;这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案&#xff1a; 对惹&#xff0c;这里有一个游戏开发交流小组&…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

【7色560页】职场可视化逻辑图高级数据分析PPT模版

7种色调职场工作汇报PPT&#xff0c;橙蓝、黑红、红蓝、蓝橙灰、浅蓝、浅绿、深蓝七种色调模版 【7色560页】职场可视化逻辑图高级数据分析PPT模版&#xff1a;职场可视化逻辑图分析PPT模版https://pan.quark.cn/s/78aeabbd92d1...

莫兰迪高级灰总结计划简约商务通用PPT模版

莫兰迪高级灰总结计划简约商务通用PPT模版&#xff0c;莫兰迪调色板清新简约工作汇报PPT模版&#xff0c;莫兰迪时尚风极简设计PPT模版&#xff0c;大学生毕业论文答辩PPT模版&#xff0c;莫兰迪配色总结计划简约商务通用PPT模版&#xff0c;莫兰迪商务汇报PPT模版&#xff0c;…...

LabVIEW双光子成像系统技术

双光子成像技术的核心特性 双光子成像通过双低能量光子协同激发机制&#xff0c;展现出显著的技术优势&#xff1a; 深层组织穿透能力&#xff1a;适用于活体组织深度成像 高分辨率观测性能&#xff1a;满足微观结构的精细研究需求 低光毒性特点&#xff1a;减少对样本的损伤…...

人工智能--安全大模型训练计划:基于Fine-tuning + LLM Agent

安全大模型训练计划&#xff1a;基于Fine-tuning LLM Agent 1. 构建高质量安全数据集 目标&#xff1a;为安全大模型创建高质量、去偏、符合伦理的训练数据集&#xff0c;涵盖安全相关任务&#xff08;如有害内容检测、隐私保护、道德推理等&#xff09;。 1.1 数据收集 描…...

数学建模-滑翔伞伞翼面积的设计,运动状态计算和优化 !

我们考虑滑翔伞的伞翼面积设计问题以及运动状态描述。滑翔伞的性能主要取决于伞翼面积、气动特性以及飞行员的重量。我们的目标是建立数学模型来描述滑翔伞的运动状态,并优化伞翼面积的设计。 一、问题分析 滑翔伞在飞行过程中受到重力、升力和阻力的作用。升力和阻力与伞翼面…...

算法打卡第18天

从中序与后序遍历序列构造二叉树 (力扣106题) 给定两个整数数组 inorder 和 postorder &#xff0c;其中 inorder 是二叉树的中序遍历&#xff0c; postorder 是同一棵树的后序遍历&#xff0c;请你构造并返回这颗 二叉树 。 示例 1: 输入&#xff1a;inorder [9,3,15,20,7…...

Spring AOP代理对象生成原理

代理对象生成的关键类是【AnnotationAwareAspectJAutoProxyCreator】&#xff0c;这个类继承了【BeanPostProcessor】是一个后置处理器 在bean对象生命周期中初始化时执行【org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization】方法时…...