Foot.cs 2.8 KB

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