MoveCharacter.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MoveCharacter : Character
  5. {
  6. public Foot foot;
  7. public float extraRiseGravity = 0; //上升时额外重力加速度
  8. public float extraFallGravity = -10; //下落时额外重力加速度
  9. public float moveSpeed = 5;
  10. //[HideInInspector]
  11. public float beRepelValue;
  12. public float totalBeRepelValue;
  13. [HideInInspector]
  14. public float weakTime;
  15. public float totalWeakTime;
  16. public float weakUpSpeed = 10f;
  17. public float decelerationRatio = 1f;
  18. public float minHurtKeepTime = 0.2f;
  19. [HideInInspector]
  20. public float hurtKeepTime = 0;
  21. public float hurtChangeVelocity = 1;
  22. [Header("新增漂浮效果参数")]
  23. public float maxTime = 0; //上升最大耗时
  24. public float minTime = 6; //上升最小耗时
  25. public float maxHeight = 12; //最大上升高度
  26. public float minHeight = 7; //最小上升高度
  27. public float maxRotateSpeed = 20; //最大旋转速度
  28. public float minRotateSpeed = 5; //最小旋转速度
  29. public float floatTime = 20; //漂浮时间
  30. private float curTime; //漂浮已进行时长
  31. private float height; //漂浮高度
  32. private float riseTime; //上升时间
  33. private float curHeight; //当前所在高度
  34. private float rotateSpeed; //旋转速度
  35. private float rotateDir; //旋转方向
  36. private Vector3 origPos; //初始位置
  37. private float rise = 1;
  38. private float down = -1;
  39. private Vector3 oneClockwise = new Vector3(0, 0, 1);
  40. private Vector3 oneCounterClockwise = new Vector3(0, 0, -1);
  41. private int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
  42. private GameObject spine;
  43. private MeshRenderer mesh;
  44. private Material[] mats;
  45. public Material[] outlineMats;
  46. private void Awake()
  47. {
  48. spine = transform.GetChild(0).GetChild(0).gameObject;
  49. mesh = spine.GetComponent<MeshRenderer>();
  50. mats = mesh.materials;
  51. }
  52. private void ChangeMat(int state)
  53. {
  54. if (spine == null || mesh == null || mats == null)
  55. {
  56. spine = transform.GetChild(0).GetChild(0).gameObject;
  57. mesh = spine.GetComponent<MeshRenderer>();
  58. mats = mesh.materials;
  59. }
  60. if (state == 0)
  61. {
  62. print(1);
  63. mesh.materials = outlineMats;
  64. }
  65. else
  66. {
  67. mesh.materials = mats;
  68. }
  69. }
  70. public void FloatStateOn()
  71. {
  72. ChangeMat(0);
  73. ChangeState(CharacterState.Rise);
  74. floatState = 1;
  75. riseTime = Random.Range(minTime, maxTime);
  76. origPos = transform.position;
  77. curHeight = origPos.y;
  78. curTime = 0;
  79. rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
  80. rotateDir = (1.5f - Random.Range(1, 3)) * 2;
  81. height = Random.Range(minHeight, maxHeight);
  82. }
  83. private void RotateSelf()
  84. {
  85. //transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  86. }
  87. private void Update()
  88. {
  89. //print(maxTime);
  90. if (floatState == 1)
  91. {
  92. RotateSelf();
  93. curTime += Time.deltaTime;
  94. aniCollider.Play("Rise", 0, 0);
  95. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  96. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  97. if (curHeight >= height - 0.02f)
  98. {
  99. floatState = 2;
  100. ChangeState(CharacterState.Float);
  101. transform.position = new Vector3(origPos.x, height, origPos.z);
  102. }
  103. }
  104. else if (floatState == 2)
  105. {
  106. RotateSelf();
  107. curTime += Time.deltaTime;
  108. transform.position = new Vector3(origPos.x, height, origPos.z);
  109. if (curTime >= floatTime)
  110. {
  111. floatState = 3;
  112. ChangeState(CharacterState.Fall);
  113. }
  114. }
  115. else if (floatState == 3)
  116. {
  117. RotateSelf();
  118. aniCollider.Play("Fall", 0, 0);
  119. if (transform.position.y >= origPos.y + 0.05f)
  120. {
  121. curHeight -= 10 * Time.deltaTime;
  122. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  123. }
  124. else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
  125. {
  126. transform.position = origPos;
  127. floatState = 0;
  128. ChangeState(CharacterState.Idle);
  129. ChangeMat(1);
  130. foreach (Material m in mats)
  131. {
  132. m.SetInt("_Outline", 0);
  133. }
  134. if (rotateDir==1)
  135. {
  136. transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneClockwise, 0.1f);
  137. }
  138. else
  139. {
  140. transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneCounterClockwise, 0.1f);
  141. }
  142. if (transform.localEulerAngles.z >= -0.02f && transform.localEulerAngles.z <= 0.02f)
  143. {
  144. transform.localEulerAngles = new Vector3(0, 0, 0);
  145. }
  146. }
  147. }
  148. }
  149. public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  150. {
  151. print("MoveCharacterBeHit");
  152. if (invincibleTime > 0)
  153. {
  154. return;
  155. }
  156. hp -= damage;
  157. uiHp.Show(hp, totalHp);
  158. if (hp <= 0)
  159. {
  160. rb.AddForce(force);
  161. ChangeState(CharacterState.Die);
  162. return;
  163. }
  164. else if (changeHurt && state == CharacterState.Weak)
  165. {
  166. print("ChangeHurt");
  167. rb.AddForce(force);
  168. ChangeState(CharacterState.Hurt);
  169. beRepelValue = totalBeRepelValue;
  170. return;
  171. }
  172. beRepelValue -= repelValue;
  173. if (changeHurt && beRepelValue <= 0)
  174. {
  175. print("ChangeWeak");
  176. ChangeState(CharacterState.Weak);
  177. }
  178. }
  179. }