Foot.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. private void Awake()
  19. {
  20. rb = GetComponentInParent<Rigidbody>();
  21. moveCharacter = rb.transform.GetComponent<MoveCharacter>();
  22. }
  23. private void FixedUpdate()
  24. {
  25. if (trigGroundList.Count == 0)
  26. {
  27. if (!haveGravity|| moveCharacter.platformY != 0)
  28. {
  29. rb.useGravity = true;
  30. haveGravity = true;
  31. moveCharacter.velocityAddition = 0;
  32. moveCharacter.platformY = 0;
  33. }
  34. }
  35. else
  36. {
  37. Platform platform = null;
  38. foreach (var item in trigGroundList)
  39. {
  40. platform = item.GetComponent<Platform>();
  41. if (platform!=null)
  42. {
  43. if (moveCharacter.transform.position.x < platform.right.position.x
  44. && moveCharacter.transform.position.x > platform.left.position.x)
  45. {
  46. break;
  47. }
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54. if (platform != null)
  55. {
  56. if (haveGravity)
  57. {
  58. if (rb.velocity.y <= 0)
  59. {
  60. rb.useGravity = false;
  61. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
  62. moveCharacter.platformY = platform.GetPosY(rb.transform.position);
  63. rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformY, rb.transform.position.z);
  64. haveGravity = false;
  65. }
  66. }
  67. else
  68. {
  69. if (platform.rb != null)
  70. {
  71. moveCharacter.velocityAddition = platform.rb.velocity.x;
  72. }
  73. moveCharacter.platformY = platform.GetPosY(rb.transform.position);
  74. }
  75. }
  76. else
  77. {
  78. if (!haveGravity)
  79. {
  80. rb.useGravity = true;
  81. haveGravity = true;
  82. }
  83. }
  84. }
  85. }
  86. private void OnTriggerEnter(Collider other)
  87. {
  88. if (other.CompareTag("Ground")|| other.CompareTag("Platform"))
  89. {
  90. if (!trigGroundList.Exists(t => t == other.gameObject))
  91. {
  92. trigGroundList.Add(other.gameObject);
  93. }
  94. }
  95. }
  96. private void OnTriggerExit(Collider other)
  97. {
  98. for (int i = 0; i < trigGroundList.Count; i++)
  99. {
  100. if (trigGroundList[i] == other.gameObject)
  101. {
  102. trigGroundList.RemoveAt(i);
  103. i--;
  104. }
  105. }
  106. }
  107. }