Unity类银河恶魔城学习记录8-5 p81 Blackhole duration源代码

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

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

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

Blackhole_Skill_Controller.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;public class Blackhole_Skill_Controller : MonoBehaviour
{[SerializeField] private GameObject hotKeyPrefab;[SerializeField] private List<KeyCode> KeyCodeList;private float maxSize;//最大尺寸private float growSpeed;//变大速度private float shrinkSpeed;//缩小速度private float blackholeTimer;private bool canGrow = true;//是否可以变大private bool canShrink;//缩小private bool canCreateHotKeys = true;专门控制后面进入的没法生成热键private bool cloneAttackReleased;private int amountOfAttacks = 4;private float cloneAttackCooldown = .3f;private float cloneAttackTimer;private List<Transform> targets = new List<Transform>();private List<GameObject> createdHotKey = new List<GameObject>();public bool playerCanExitState { get; private set; }public void SetupBlackhole(float _maxSize,float _growSpeed,float _shrinkSpeed,int _amountOfAttacks,float _cloneAttackCooldown,float _blackholeDuration){maxSize = _maxSize;growSpeed = _growSpeed;shrinkSpeed = _shrinkSpeed;amountOfAttacks = _amountOfAttacks;cloneAttackCooldown = _cloneAttackCooldown;blackholeTimer = _blackholeDuration;}private void Update(){blackholeTimer -= Time.deltaTime;cloneAttackTimer -= Time.deltaTime;if(blackholeTimer <= 0){blackholeTimer = Mathf.Infinity;//防止重复检测if (targets.Count > 0)//只有有target才释放攻击{ReleaseCloneAttack();//释放攻击}elseFinishBlackholeAbility();//缩小黑洞}if (Input.GetKeyDown(KeyCode.R)&& targets.Count > 0){ReleaseCloneAttack();}CloneAttackLogic();if (canGrow && !canShrink){//这是控制物体大小的参数transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);//类似MoveToward,不过是放大到多少大小 https://docs.unity3d.com/cn/current/ScriptReference/Vector2.Lerp.html}if (canShrink){transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(0, 0), shrinkSpeed * Time.deltaTime);if (transform.localScale.x <= 1f){Destroy(gameObject);}}}private void ReleaseCloneAttack(){cloneAttackReleased = true;canCreateHotKeys = false;DestroyHotKeys();PlayerManager.instance.player.MakeTransprent(true);}private void CloneAttackLogic(){if (cloneAttackTimer < 0 && cloneAttackReleased&&amountOfAttacks>0){cloneAttackTimer = cloneAttackCooldown;int randomIndex = Random.Range(0, targets.Count);//限制攻击次数和设置攻击偏移量float _offset;if (Random.Range(0, 100) > 50)_offset = 1.5f;else_offset = -1.5f;SkillManager.instance.clone.CreateClone(targets[randomIndex], new Vector3(_offset, 0, 0));amountOfAttacks--;if (amountOfAttacks <= 0){Invoke("FinishBlackholeAbility", 0.5f);}}}private void FinishBlackholeAbility(){DestroyHotKeys();canShrink = true;cloneAttackReleased = false;playerCanExitState = true;}private void OnTriggerEnter2D(Collider2D collision){if(collision.GetComponent<Enemy>()!=null){collision.GetComponent<Enemy>().FreezeTime(true);CreateHotKey(collision);}}private void OnTriggerExit2D(Collider2D collision){if (collision.GetComponent<Enemy>() != null){collision.GetComponent<Enemy>().FreezeTime(false);}}private void CreateHotKey(Collider2D collision){if(KeyCodeList.Count == 0)//当所有的KeyCode都被去除,就不在创建实例{return;}if(!canCreateHotKeys)//这是当角色已经开大了,不在创建实例{return;}//创建实例GameObject newHotKey = Instantiate(hotKeyPrefab, collision.transform.position + new Vector3(0, 2), Quaternion.identity);//将实例添加进列表createdHotKey.Add(newHotKey);//随机KeyCode传给HotKey,并且传过去一个毁掉一个KeyCode choosenKey = KeyCodeList[Random.Range(0, KeyCodeList.Count)];KeyCodeList.Remove(choosenKey);Blackhole_Hotkey_Controller newHotKeyScript = newHotKey.GetComponent<Blackhole_Hotkey_Controller>();newHotKeyScript.SetupHotKey(choosenKey, collision.transform, this);}public void AddEnemyToList(Transform _myEnemy){targets.Add(_myEnemy);}//销毁Hotkeyprivate void DestroyHotKeys(){if(createdHotKey.Count <= 0){return;}for (int i = 0; i < createdHotKey.Count; i++){Destroy(createdHotKey[i]); }}}

Blackhole_Skill.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Blackhole_Skill : Skill
{[SerializeField]private float maxSize;//最大尺寸[SerializeField] private float growSpeed;//变大速度[SerializeField] private float shrinkSpeed;//缩小速度[SerializeField] private GameObject blackholePrefab;[Space][SerializeField] private float blackholeDuration;[SerializeField] int amountOfAttacks = 4;[SerializeField] float cloneAttackCooldown = .3f;Blackhole_Skill_Controller currentBlackhole;public override bool CanUseSkill(){return base.CanUseSkill();}public override void UseSkill(){base.UseSkill();GameObject newBlackhole = Instantiate(blackholePrefab,player.transform.position,Quaternion.identity);currentBlackhole = newBlackhole.GetComponent<Blackhole_Skill_Controller>();currentBlackhole.SetupBlackhole(maxSize,growSpeed,shrinkSpeed,amountOfAttacks,cloneAttackCooldown,blackholeDuration);}protected override void Start(){base.Start();}protected override void Update(){base.Update();}public bool SkillCompleted(){if(currentBlackhole == null)return false;if (currentBlackhole.playerCanExitState){return true;}else{return false;}}
}
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 float counterAttackDuration = .2f;public bool isBusy{ get; private set; }//防止在攻击间隔中进入move//[Header("Move Info")]public float moveSpeed;//定义速度,与xInput相乘控制速度的大小public float jumpForce;public float swordReturnImpact;//在player里设置swordReturnImpact作为击退的参数[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; }public PlayerCounterAttackState counterAttack { get; private set; }public PlayerAimSwordState aimSword { get; private set; }public PlayerCatchSwordState catchSword { get; private set; }public PlayerBlackholeState blackhole { get; private set; }public SkillManager skill { get; private set; }public GameObject sword{ get; private set; }//声明sword#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");counterAttack = new PlayerCounterAttackState(this, stateMachine, "CounterAttack");aimSword = new PlayerAimSwordState(this,stateMachine, "AimSword");catchSword = new PlayerCatchSwordState(this, stateMachine, "CatchSword");blackhole = new PlayerBlackholeState(this, stateMachine, "Jump");//this 就是 Player这个类本身}//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)protected override void Start(){base.Start();stateMachine.Initialize(idleState);skill = SkillManager.instance;}protected override void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update{base.Update();stateMachine.currentState.Update();//反复调用CurrentState的Update函数CheckForDashInput();}public void AssignNewSword(GameObject _newSword)//保持创造的sword实例的函数{sword = _newSword;}public void CatchTheSword()//通过player的CatchTheSword进入,及当剑消失的瞬间进入{stateMachine.ChangeState(catchSword);Destroy(sword);}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(){if (IsWallDetected()){return;}//修复在wallslide可以dash的BUGif (Input.GetKeyDown(KeyCode.LeftShift) && skill.dash.CanUseSkill())//将DashTimer<0 的判断 改成DashSkill里的判断{dashDir = Input.GetAxisRaw("Horizontal");//设置一个值,可以将dash的方向改为你想要的方向而不是你的朝向if (dashDir == 0){dashDir = facingDir;//只有当玩家没有控制方向时才使用默认朝向}stateMachine.ChangeState(dashState);}}//将Dash切换设置成一个函数,使其在所以情况下都能使用}

PlayerBlackholeState.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class PlayerBlackholeState : PlayerState
{private float flyTime = .4f;//飞行时间private bool skillUsed;//技能是否在被使用private float defaultGravity;public PlayerBlackholeState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void AnimationFinishTrigger(){base.AnimationFinishTrigger();}public override void Enter(){base.Enter();skillUsed = false;stateTimer = flyTime;defaultGravity = rb.gravityScale;rb.gravityScale = 0;}public override void Exit(){base.Exit();rb.gravityScale = defaultGravity;player.MakeTransprent(false);}public override void Update(){base.Update();//使角色释放技能后能飞起来if (stateTimer > 0){rb.velocity = new Vector2(0, 15);}if(stateTimer < 0){rb.velocity = new Vector2(0, -.1f);if(!skillUsed){if(player.skill.blackhole.CanUseSkill())//创建实体skillUsed = true;}}if(player.skill.blackhole.SkillCompleted()){stateMachine.ChangeState(player.airState);}}}

这篇关于Unity类银河恶魔城学习记录8-5 p81 Blackhole duration源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

线性代数|机器学习-P36在图中找聚类

文章目录 1. 常见图结构2. 谱聚类 感觉后面几节课的内容跨越太大,需要补充太多的知识点,教授讲得内容跨越较大,一般一节课的内容是书本上的一章节内容,所以看视频比较吃力,需要先预习课本内容后才能够很好的理解教授讲解的知识点。 1. 常见图结构 假设我们有如下图结构: Adjacency Matrix:行和列表示的是节点的位置,A[i,j]表示的第 i 个节点和第 j 个