Foot.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. public class Foot : MonoBehaviour
  6. {
  7. [FoldoutGroup("×é¼þ")] public Rigidbody rb;
  8. [FoldoutGroup("×é¼þ")] public MoveCharacter moveCharacter;
  9. public bool haveGravity = true;
  10. public bool TrigGround
  11. {
  12. get
  13. {
  14. return trigGroundList.Count > 0;
  15. }
  16. }
  17. public List<GameObject> trigGroundList;
  18. [DisplayOnly] public Platform platform;
  19. private void Awake()
  20. {
  21. rb = GetComponentInParent<Rigidbody>();
  22. moveCharacter = rb.transform.GetComponent<MoveCharacter>();
  23. }
  24. private void FixedUpdate()
  25. {
  26. if (trigGroundList.Count == 0)
  27. {
  28. if (!haveGravity|| moveCharacter.platformPosY != 0 || moveCharacter.platformRotZ!=0)
  29. {
  30. rb.useGravity = true;
  31. haveGravity = true;
  32. moveCharacter.velocityAddition = 0;
  33. moveCharacter.platformPosY = 0;
  34. moveCharacter.platformRotZ = 0;
  35. }
  36. }
  37. else
  38. {
  39. platform = null;
  40. foreach (var item in trigGroundList)
  41. {
  42. Platform newPlatform = item.GetComponent<Platform>();
  43. if (newPlatform != null)
  44. {
  45. if (moveCharacter.transform.position.x < newPlatform.right.position.x
  46. && moveCharacter.transform.position.x > newPlatform.left.position.x)
  47. {
  48. if(platform == null)
  49. {
  50. platform = newPlatform;
  51. }
  52. else if (newPlatform.haveMagnetic)
  53. {
  54. platform = newPlatform;
  55. }
  56. }
  57. }
  58. }
  59. if (platform != null)
  60. {
  61. if (haveGravity)
  62. {
  63. if (rb.velocity.y <= 0)
  64. {
  65. rb.useGravity = false;
  66. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
  67. moveCharacter.platformPosY = platform.GetPosY(rb.transform.position);
  68. float targetRotZ = platform.GetRotZ();
  69. if (targetRotZ != moveCharacter.platformRotZ)
  70. {
  71. moveCharacter.platformRotZ = targetRotZ;
  72. moveCharacter.RotLerpTime = 0;
  73. }
  74. rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformPosY, rb.transform.position.z);
  75. //rb.transform.rotation = Quaternion.Euler(0, 0, moveCharacter.platformRotZ);
  76. haveGravity = false;
  77. }
  78. }
  79. else
  80. {
  81. if (platform.rb != null)
  82. {
  83. moveCharacter.velocityAddition = platform.rb.velocity.x;
  84. }
  85. moveCharacter.platformPosY = platform.GetPosY(rb.transform.position);
  86. float targetRotZ = platform.GetRotZ();
  87. if (targetRotZ != moveCharacter.platformRotZ)
  88. {
  89. moveCharacter.platformRotZ = targetRotZ;
  90. moveCharacter.RotLerpTime = 0;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (!haveGravity)
  97. {
  98. rb.useGravity = true;
  99. haveGravity = true;
  100. }
  101. }
  102. }
  103. }
  104. private void OnTriggerEnter(Collider other)
  105. {
  106. if (other.CompareTag("Ground")|| other.CompareTag("Platform"))
  107. {
  108. if (!trigGroundList.Exists(t => t == other.gameObject))
  109. {
  110. trigGroundList.Add(other.gameObject);
  111. Platform platform = other.GetComponent<Platform>();
  112. if (platform!=null)
  113. {
  114. platform.feet.Add(this);
  115. }
  116. }
  117. }
  118. }
  119. private void OnTriggerExit(Collider other)
  120. {
  121. for (int i = 0; i < trigGroundList.Count; i++)
  122. {
  123. if (trigGroundList[i] == other.gameObject)
  124. {
  125. trigGroundList.RemoveAt(i);
  126. i--;
  127. Platform platform = other.GetComponent<Platform>();
  128. if (platform != null)
  129. {
  130. platform.feet.Remove(this);
  131. }
  132. }
  133. }
  134. }
  135. }