ESpirits_Float.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ESpirits_Float : MonoBehaviour
  5. {
  6. [Header("出场飞升")]
  7. private bool isRise = true;
  8. public float height; //升到哪个高度后停止
  9. private float curHeight;
  10. private float speed = 1;
  11. private Vector3 origPos;
  12. [Header("漂浮锁定玩家")]
  13. public float intervalTime = 10f; //漂浮间隔时间
  14. private Vector3 attackPos; //漂浮中心
  15. public float followTime = 3; //跟随玩家时间
  16. public float lockTime = 1.5f; //锁定等待时间
  17. //public float attackTime = 10f; //漂浮持续时间
  18. public GameObject fe; //漂浮脚本
  19. public GameObject lockEffect; //锁定特效
  20. private GameObject curFe;
  21. private GameObject curLock;
  22. private float pastTime; //已经经过的时间
  23. private GameObject player1;
  24. private GameObject player2;
  25. private GameObject target;
  26. private bool isDoing = false;
  27. private bool isFollow = false;
  28. private bool isLock = false;
  29. private bool isAttack = false;
  30. private Enemy ene;
  31. private void Awake()
  32. {
  33. ene = GetComponent<Enemy>();
  34. curLock = Instantiate(lockEffect, new Vector3(0, -50, 0), new Quaternion(0, 0, 0, 0), null);
  35. curFe = Instantiate(fe, curLock.transform.position, new Quaternion(0, 0, 0, 0), null);
  36. curFe.GetComponent<FloatEffect>().isEnemy = true;
  37. curFe.SetActive(false);
  38. curLock.SetActive(false);
  39. player1 = PlayersInput.instance[0].gameObject;
  40. //player2 = PlayersInput.instance[1].gameObject;
  41. }
  42. private void Start()
  43. {
  44. origPos = transform.position;
  45. curHeight = origPos.y;
  46. }
  47. private void OnDisable()
  48. {
  49. curFe.SetActive(false);
  50. curLock.SetActive(false);
  51. }
  52. private void MoveToHeight()
  53. {
  54. curHeight = Mathf.SmoothDamp(curHeight, height, ref speed, 0.02f);
  55. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  56. }
  57. private void ChoosePlayer()
  58. {
  59. /*
  60. float dis1 = Vector2.Distance(player1.transform.position, transform.position);
  61. float dis2 = Vector2.Distance(player2.transform.position, transform.position);
  62. if (dis1 < dis2)
  63. {
  64. target = player1;
  65. }
  66. else
  67. {
  68. target = player2;
  69. }*/
  70. target = player1;
  71. }
  72. private void FollowPlayer()
  73. {
  74. curLock.transform.position = target.transform.position;
  75. }
  76. private void LockPlayer()
  77. {
  78. }
  79. public void FloatAttack()
  80. {
  81. curFe.transform.position = curLock.transform.position;
  82. curLock.SetActive(false);
  83. curFe.SetActive(true);
  84. }
  85. private void Update()
  86. {
  87. //出场飞升
  88. if (isRise)
  89. {
  90. MoveToHeight();
  91. if (curHeight - height <= 0.02f && curHeight - height >= -0.02f)
  92. {
  93. isRise = false;
  94. curHeight = height;
  95. transform.position = new Vector3(origPos.x, height, origPos.z);
  96. }
  97. }
  98. //隔一段时间发动一次锁定攻击
  99. if (!isRise && !isDoing && (ene.state == CharacterState.Idle || ene.state == CharacterState.Run || ene.state == CharacterState.Attack))
  100. {
  101. pastTime += Time.deltaTime;
  102. if (pastTime >= intervalTime)
  103. {
  104. ChoosePlayer();
  105. isDoing = true;
  106. isFollow = true;
  107. pastTime = 0;
  108. curLock.SetActive(true);
  109. }
  110. }
  111. //跟随玩家
  112. if (isFollow)
  113. {
  114. pastTime += Time.deltaTime;
  115. FollowPlayer();
  116. if (pastTime >= followTime)
  117. {
  118. isFollow = false;
  119. isLock = true;
  120. pastTime = 0;
  121. }
  122. }
  123. //锁定范围
  124. if (isLock)
  125. {
  126. pastTime += Time.deltaTime;
  127. LockPlayer();
  128. if (pastTime >= lockTime)
  129. {
  130. isLock = false;
  131. isAttack = true;
  132. pastTime = 0;
  133. }
  134. }
  135. //攻击
  136. if (isAttack)
  137. {
  138. FloatAttack();
  139. isAttack = false;
  140. isDoing = false;
  141. }
  142. }
  143. }