Foot.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Foot : MonoBehaviour
  5. {
  6. public Rigidbody rb;
  7. public bool haveGravity = true;
  8. public bool TrigGround
  9. {
  10. get
  11. {
  12. return trigGroundList.Count > 0;
  13. }
  14. }
  15. public List<GameObject> trigGroundList;
  16. public BaGuaTrigger myBaGuaTrigger; //×Ô¼º
  17. public List<BaGuaTrigger> baGuaTrigger = new List<BaGuaTrigger>(); //½ÅÏÂµÄ°ËØÔÅÖ×Ó
  18. public bool notOnGiant;
  19. private void Update()
  20. {
  21. rb = GetComponentInParent<Rigidbody>();
  22. if (trigGroundList.Count == 0)
  23. {
  24. if (!haveGravity)
  25. {
  26. rb.useGravity = true;
  27. haveGravity = true;
  28. rb.GetComponent<MoveCharacter>().velocityAddition = 0;
  29. }
  30. }
  31. else
  32. {
  33. if (haveGravity)
  34. {
  35. if (rb.velocity.y < 0)
  36. {
  37. foreach (GameObject i in trigGroundList)
  38. {
  39. if (i.layer == 13)
  40. {
  41. rb.useGravity = false;
  42. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
  43. haveGravity = false;
  44. rb.transform.position = new Vector3
  45. (rb.transform.position.x, i.transform.position.y, rb.transform.position.z);
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. else
  52. {
  53. Platform platform = trigGroundList[0].GetComponent<Platform>();
  54. if (platform != null && platform.rb != null)
  55. {
  56. rb.GetComponent<MoveCharacter>().velocityAddition = platform.rb.velocity.x;
  57. }
  58. }
  59. }
  60. }
  61. private void FixedUpdate()
  62. {
  63. for (int i = 0; i < trigGroundList.Count; i++)
  64. {
  65. if (trigGroundList[i] == null)
  66. {
  67. trigGroundList.RemoveAt(i);
  68. i--;
  69. }
  70. }
  71. }
  72. private void OnTriggerEnter(Collider other)
  73. {
  74. if (other.CompareTag("Plane"))
  75. {
  76. if (notOnGiant)
  77. {
  78. Platform platform = other.gameObject.GetComponent<Platform>();
  79. if (platform)
  80. {
  81. return;
  82. }
  83. }
  84. if (!trigGroundList.Exists(t => t == other.gameObject))
  85. {
  86. trigGroundList.Add(other.gameObject);
  87. }
  88. }
  89. }
  90. private void OnTriggerExit(Collider other)
  91. {
  92. for (int i = 0; i < trigGroundList.Count; i++)
  93. {
  94. if (trigGroundList[i] == other.gameObject)
  95. {
  96. trigGroundList.RemoveAt(i);
  97. i--;
  98. }
  99. }
  100. }
  101. }