当前位置:   article > 正文

类银河恶魔城学习记录1-5 CollisionCheck源代码 P32

类银河恶魔城学习记录1-5 CollisionCheck源代码 P32
Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

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

Player.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. public class Player : MonoBehaviour
  6. {
  7. [Header("Move Info")]
  8. public float moveSpeed;//定义速度,与xInput相乘控制速度的大小
  9. public float jumpForce;
  10. [Header("Collision Info")]
  11. [SerializeField] private Transform groundCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置
  12. [SerializeField] private float groundCheckDistance;
  13. [SerializeField] private Transform wallCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置
  14. [SerializeField] private float wallCheckDistance;
  15. [SerializeField] private LayerMask whatIsGround;//LayerMask类,与Raycast配合,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html
  16. #region 定义Unity组件
  17. public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权
  18. public Rigidbody2D rb { get; private set; }//配合拿到身上的Rigidbody2D组件控制权
  19. #endregion
  20. #region 定义States
  21. public PlayerStateMachine stateMachine { get; private set; }
  22. public PlayerIdleState idleState { get; private set; }
  23. public PlayerMoveState moveState { get; private set; }
  24. public PlayerJumpState jumpState { get; private set; }
  25. public PlayerAirState airState { get; private set; }//与其说是airState,不如说是FallState
  26. #endregion
  27. private void Awake()
  28. {
  29. stateMachine = new PlayerStateMachine();
  30. //通过构造函数,在构造时传递信息
  31. idleState = new PlayerIdleState(this, stateMachine, "Idle");
  32. moveState = new PlayerMoveState(this, stateMachine, "Move");
  33. jumpState = new PlayerJumpState(this, stateMachine, "Jump");
  34. airState = new PlayerAirState(this, stateMachine, "Jump");
  35. //this 就是 Player这个类本身
  36. }//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)
  37. private void Start()
  38. {
  39. anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权
  40. rb = GetComponent<Rigidbody2D>();
  41. stateMachine.Initialize(idleState);
  42. }
  43. private void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update
  44. {
  45. stateMachine.currentState.Update();//反复调用CurrentState的Update函数
  46. }
  47. public void SetVelocity(float _xVelocity,float _yVelocity)
  48. {
  49. rb.velocity = new Vector2(_xVelocity, _yVelocity);//将rb的velocity属性设置为对应的想要的二维向量。因为2D游戏的速度就是二维向量
  50. }//控制速度的函数,此函数在其他State中可能会使用,但仅能通过player.SeVelocity调用
  51. public bool IsGroundDetected()
  52. {
  53. return Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);
  54. }//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html
  55. //xxxxxxxx() => xxxxxxxx == xxxxxxxxxx() return xxxxxxxxx;
  56. private void OnDrawGizmos()
  57. {
  58. Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。
  59. Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y ));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。
  60. }//画线函数
  61. }

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

PlayerStateMachine.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerStateMachine
  5. {
  6. public PlayerState currentState { get; private set; }
  7. public void Initialize(PlayerState _startState)
  8. {
  9. currentState = _startState;
  10. currentState.Enter();
  11. }//初始化状态函数,及通过将idleState传送进来,以便于调用idleState中的Enter函数
  12. public void ChangeState(PlayerState _newState)
  13. {
  14. currentState.Exit();
  15. currentState = _newState;
  16. currentState.Enter();
  17. }//更改状态函数
  18. //1.调用当前状态的Exit函数,使动画为false
  19. //2.传入新的状态,替换原来的状态
  20. //3.调用新的状态的Enter函数
  21. }

PlayerGroundState.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //GroundState用于保证只有在Idle和Move这两个地面状态下才能调用某些函数,并且稍微减少一点代码量
  5. public class PlayerGroundState : PlayerState
  6. {
  7. public PlayerGroundState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
  8. {
  9. }
  10. public override void Enter()
  11. {
  12. base.Enter();
  13. }
  14. public override void Update()
  15. {
  16. base.Update();
  17. if (Input.GetKeyDown(KeyCode.Space)&&player.IsGroundDetected())
  18. {
  19. stateMachine.ChangeState(player.jumpState);
  20. }//空格切换为跳跃状态
  21. }
  22. public override void Exit()
  23. {
  24. base.Exit();
  25. }
  26. }

PlayerIdleState.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerIdleState : PlayerGroundState//继承函数,获得PlayerState里的所有函数和参数
  5. {
  6. public PlayerIdleState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
  7. {
  8. }//构造函数,用于传递信息。
  9. //当外补New出对象时,New出的对象里传入参数
  10. public override void Enter()
  11. {
  12. base.Enter();
  13. }
  14. public override void Exit()
  15. {
  16. base.Exit();
  17. }
  18. public override void Update()
  19. {
  20. base.Update();
  21. if(xInput != 0)
  22. {
  23. stateMachine.ChangeState(player.moveState);//在这里我们能使用Player里的东西,主要是因为我们通过构造函数传过来Player实体,如果不传,则无法使用已经存在了的Player实体。
  24. }
  25. }
  26. }

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

PlayerJumpState.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerJumpState : PlayerState
  5. {
  6. public PlayerJumpState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
  7. {
  8. }
  9. public override void Enter()
  10. {
  11. base.Enter();
  12. rb.velocity = new Vector2(rb.velocity.x, player.jumpForce);//将y轴速度改变
  13. }
  14. public override void Update()
  15. {
  16. base.Update();
  17. if (rb.velocity.y < 0)//当速度小于0时切换为airState
  18. {
  19. stateMachine.ChangeState(player.airState);//与其说是airState,不如说是FallState
  20. }
  21. }
  22. public override void Exit()
  23. {
  24. base.Exit();
  25. }
  26. }

PlayerAirState.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerAirState : PlayerState
  5. {
  6. public PlayerAirState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
  7. {
  8. }
  9. public override void Enter()
  10. {
  11. base.Enter();
  12. }
  13. public override void Update()
  14. {
  15. base.Update();
  16. if (player.IsGroundDetected())
  17. {
  18. stateMachine.ChangeState(player.idleState);
  19. }//暂时使用,因为这样写是由错误的
  20. }
  21. public override void Exit()
  22. {
  23. base.Exit();
  24. }
  25. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/58592
推荐阅读
相关标签
  

闽ICP备14008679号