当前位置:   article > 正文

Unity 麦扣 x 勇士传说 全解析 之 怪物基类(2)(附各模块知识的链接,零基础也包学会的牢弟)(案例难度:★★☆☆☆)

Unity 麦扣 x 勇士传说 全解析 之 怪物基类(2)(附各模块知识的链接,零基础也包学会的牢弟)(案例难度:★★☆☆☆)

1.怪物的动画逻辑一览

2.怪物的受伤死亡逻辑一览

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using UnityEngine;
  5. public class Monster : MonoBehaviour
  6. {
  7. [Header("速度")]
  8. public float normalSpeed;
  9. public float chaseSpeed;
  10. public float currentSpeed;
  11. [Header("面朝向")]
  12. protected Vector3 headFor;
  13. [Header("等待计时器")]
  14. public float waitTime;
  15. public float waitClock;
  16. public bool waitState;
  17. [Header("组件")]
  18. protected Rigidbody2D rb;
  19. protected Animator animator;
  20. protected PhysicsCheck physicscheck;
  21. private bool isWalk;
  22. private bool run;
  23. //当前移动
  24. private Transform attacker;
  25. //怪物受伤
  26. private bool isHurt;
  27. //受伤受到击退的力
  28. public float hurtForce;
  29. //死亡
  30. public bool isDead;
  31. private void Awake() {
  32. rb = GetComponent<Rigidbody2D>();
  33. animator = GetComponent<Animator>();
  34. physicscheck = GetComponent<PhysicsCheck>();
  35. }
  36. private void Update() {
  37. //实时面朝向
  38. headFor = new Vector3(-this.transform.localScale.x,0,0);
  39. if ((headFor.x > 0 && physicscheck.isRightWall) || (headFor.x < 0 && physicscheck.isLeftWall)) {
  40. waitState = true;
  41. animator.SetBool("walk", false);
  42. }
  43. //延迟转身
  44. WaitAndTurn();
  45. }
  46. private void FixedUpdate() {
  47. if (!isHurt&&!isDead)
  48. {
  49. Move();
  50. }
  51. }
  52. //移动方法
  53. protected virtual void Move()
  54. {
  55. //移动算式
  56. rb.velocity=new Vector2( currentSpeed* headFor.x*Time.deltaTime,rb.velocity.y);
  57. if(!(physicscheck.isLeftWall&& physicscheck.isRightWall))
  58. {isWalk = true;
  59. animator.SetBool("walk", isWalk); }
  60. else
  61. {
  62. isWalk = false;
  63. }
  64. }
  65. //撞墙等待转身方法
  66. protected void WaitAndTurn()
  67. {
  68. if(waitState)
  69. {
  70. waitClock -= Time.deltaTime;
  71. if(waitClock<= 0)
  72. {
  73. waitState=false;
  74. waitClock = waitTime; // 重置计时器
  75. this.transform.localScale = new Vector3(headFor.x,1,1);
  76. }
  77. }
  78. }
  79. //受击后转身
  80. public void OnTakeDamage(Transform attcakTrans)
  81. {
  82. attacker = attcakTrans;
  83. if (attcakTrans.position.x - this.transform.position.x > 0)
  84. transform.localScale = new Vector3(-1, 1, 1);
  85. if (attcakTrans.position.x - this.transform.position.x < 0)
  86. transform.localScale = new Vector3(1, 1, 1);
  87. //受伤动画
  88. isHurt =true;
  89. animator.SetTrigger("IsHurt");
  90. //加力方向
  91. Vector2 dir = new Vector2(this.transform.position.x- attcakTrans.transform.position.x,0).normalized;
  92. StartCoroutine(OnHurt(dir));
  93. }
  94. private IEnumerator OnHurt(Vector2 dir)
  95. {
  96. rb.velocity = Vector2.zero; // 重置速度
  97. rb.AddForce(dir * hurtForce, ForceMode2D.Impulse);
  98. Debug.Log("Force applied: " + (dir * hurtForce)); // 添加调试信息
  99. yield return new WaitForSeconds(0.5f);
  100. isHurt = false;
  101. }
  102. public void Ondead() {
  103. isDead = true;
  104. animator.SetBool("dead",isDead);
  105. }
  106. //死亡后销毁怪物
  107. public void DestoryMonseter()
  108. {
  109. Destroy(this.gameObject);
  110. }
  111. }

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

闽ICP备14008679号