当前位置:   article > 正文

Unity类银河恶魔城学习记录4-1,4-2 Attack Logic,Collider‘s collision excepetion源代码 P54 p55_unity和c#创建高级银河恶魔城游戏

unity和c#创建高级银河恶魔城游戏
Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

Entity.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Entity : MonoBehaviour
  5. {
  6. [Header("Collision Info")]
  7. public Transform attackCheck;//transform类,代表的时物体的位置,用来控制攻击检测的位置
  8. public float attackCheckRadius;//检测半径
  9. [SerializeField] protected Transform groundCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置
  10. [SerializeField] protected float groundCheckDistance;
  11. [SerializeField] protected Transform wallCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置
  12. [SerializeField] protected float wallCheckDistance;
  13. [SerializeField] protected LayerMask whatIsGround;//LayerMask类,与Raycast配合,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html
  14. #region 定义Unity组件
  15. public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权
  16. public Rigidbody2D rb { get; private set; }//配合拿到身上的Rigidbody2D组件控制权
  17. #endregion
  18. public int facingDir { get; private set; } = 1;
  19. protected bool facingRight = true;//判断是否朝右
  20. protected virtual void Awake()
  21. {
  22. anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权
  23. rb = GetComponent<Rigidbody2D>();
  24. }
  25. protected virtual void Start()
  26. {
  27. }
  28. protected virtual void Update()
  29. {
  30. }
  31. public virtual void Damage()
  32. {
  33. Debug.Log(gameObject.name+"was damaged");
  34. }
  35. #region 速度函数Velocity
  36. public virtual void SetZeroVelocity()
  37. {
  38. rb.velocity = new Vector2(0, 0);
  39. }//设置速度为0函数
  40. public virtual void SetVelocity(float _xVelocity, float _yVelocity)
  41. {
  42. rb.velocity = new Vector2(_xVelocity, _yVelocity);//将rb的velocity属性设置为对应的想要的二维向量。因为2D游戏的速度就是二维向量
  43. FlipController(_xVelocity);//在其他设置速度的时候调用翻转控制器
  44. }//控制速度的函数,此函数在其他State中可能会使用,但仅能通过player.SeVelocity调用
  45. #endregion
  46. #region 翻转函数Flip
  47. public virtual void Flip()
  48. {
  49. facingDir = facingDir * -1;
  50. facingRight = !facingRight;
  51. transform.Rotate(0, 180, 0);//旋转函数,transform不需要额外定义,因为他是自带的
  52. }//翻转函数
  53. public virtual void FlipController(float _x)//目前设置x,目的时能在空中时也能转身
  54. {
  55. if (_x > 0 && !facingRight)//当速度大于0且没有朝右时,翻转
  56. {
  57. Flip();
  58. }
  59. else if (_x < 0 && facingRight)
  60. {
  61. Flip();
  62. }
  63. }
  64. #endregion
  65. #region 碰撞函数Collision
  66. public virtual bool IsGroundDetected()
  67. {
  68. return Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);
  69. }//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics2D.Raycast.html
  70. //xxxxxxxx() => xxxxxxxx == xxxxxxxxxx() return xxxxxxxxx;
  71. public virtual bool IsWallDetected()
  72. {
  73. return Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround);
  74. }//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics2D.Raycast.html
  75. //xxxxxxxx() => xxxxxxxx == xxxxxxxxxx() return xxxxxxxxx;
  76. protected virtual void OnDrawGizmos()
  77. {
  78. Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。
  79. Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。
  80. Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);//https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Gizmos.DrawWireSphere.html
  81. //绘制具有中心和半径的线框球体。
  82. }//画图函数
  83. #endregion
  84. }
PlayerAnimationTrigger.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class playerAnimationTriggers : MonoBehaviour
  5. {
  6. private Player player => GetComponentInParent<Player>();//获得夫组件上的实际存在的Player组件
  7. private void AnimationTrigger()
  8. {
  9. player.AnimationTrigger();
  10. }
  11. private void AttackTrigger()
  12. {
  13. Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);//创建一个碰撞器组,保存所有圈所碰到的碰撞器
  14. //https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Physics2D.OverlapCircleAll.html
  15. foreach(var hit in colliders)//https://blog.csdn.net/m0_52358030/article/details/121722077
  16. {
  17. if(hit.GetComponent<Enemy>()!=null)
  18. {
  19. hit.GetComponent<Enemy>().Damage();
  20. }
  21. }
  22. }
  23. }
Enemy_SkeletonAnimationTrigger.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Enemy_SkeletonAnimationTriggers : MonoBehaviour
  5. {
  6. private Enemy_Skeleton enemy => GetComponentInParent<Enemy_Skeleton>();//拿到enemy实体
  7. private void AnimationTrigger()
  8. {
  9. enemy.AnimationFinishTrigger();//调用实体上的函数,使triggerCalled为true;
  10. }
  11. private void AttackTrigger()
  12. {
  13. Collider2D[] colliders = Physics2D.OverlapCircleAll(enemy.attackCheck.position, enemy.attackCheckRadius);//创建一个碰撞器组,保存所有圈所碰到的碰撞器
  14. //https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Physics2D.OverlapCircleAll.html
  15. foreach (var hit in colliders)//https://blog.csdn.net/m0_52358030/article/details/121722077
  16. {
  17. if (hit.GetComponent<Player>() != null)
  18. {
  19. hit.GetComponent<Player>().Damage();
  20. }
  21. }
  22. }
  23. }
SkeletonBattleState.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. //从ground进来的
  6. public class SkeletonBattleState : EnemyState
  7. {
  8. private Transform player;//用于给Player定位,好判断怎么跟上他
  9. private Enemy_Skeleton enemy;
  10. private int moveDir;
  11. public SkeletonBattleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Skeleton _enemy ) : base(_enemyBase, _stateMachine, _animBoolName)
  12. {
  13. enemy = _enemy;
  14. }
  15. public override void Enter()
  16. {
  17. base.Enter();
  18. player = GameObject.Find("Player").transform;//全局找Player位置
  19. }
  20. public override void Exit()
  21. {
  22. base.Exit();
  23. }
  24. public override void Update()
  25. {
  26. base.Update();
  27. //退出此状态的方式
  28. if(enemy.IsPlayerDetected())
  29. {
  30. stateTimer = enemy.battleTime;
  31. if (enemy.IsPlayerDetected().distance < enemy.attackDistance)//当距离小于攻击距离,变为攻击状态
  32. {
  33. if (CanAttack())
  34. stateMachine.ChangeState(enemy.attackState);
  35. }
  36. }
  37. else//当没有看见player后,才会根据没有看到的时间来使其退出battle状态
  38. {
  39. if(stateTimer < 0||Vector2.Distance(player.transform.position,enemy.transform.position)>7)//根据距离来判断是否结束battle状态
  40. {
  41. stateMachine.ChangeState(enemy.idleState);
  42. }
  43. }
  44. //下面为移动方向设置
  45. if(player.position.x > enemy.transform.position.x)//在右,向右移动
  46. {
  47. moveDir = 1;
  48. }
  49. else if(player.position.x<enemy.transform.position.x)//在左,向左移动
  50. {
  51. moveDir = -1;
  52. }
  53. if(Vector2.Distance(player.transform.position,enemy.transform.position)>1)
  54. enemy.SetVelocity(enemy.moveSpeed * moveDir, rb.velocity.y);
  55. else
  56. {
  57. enemy.SetZeroVelocity();
  58. }//我自己设置了一个敌人接近一定距离就停下来的设置,防止出现敌人乱晃的情况
  59. }
  60. private bool CanAttack()
  61. {
  62. if(Time.time > enemy.lastTimeAttacked + enemy.attackCooldown)
  63. {
  64. enemy.lastTimeAttacked = Time.time;
  65. return true;
  66. }
  67. return false;
  68. }
  69. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号