Foot.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Awake()
  17. {
  18. rb = GetComponentInParent<Rigidbody>();
  19. }
  20. private void Update()
  21. {
  22. if (trigGroundList.Count == 0)
  23. {
  24. if (!haveGravity)
  25. {
  26. rb.useGravity = true;
  27. haveGravity = true;
  28. rb.GetComponent<MoveCharacter>().velocityAddition = Vector3.zero;
  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)
  55. {
  56. rb.GetComponent<MoveCharacter>().velocityAddition = rb.velocity;
  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. trigGroundList.Add(other.gameObject);
  77. }
  78. }
  79. private void OnTriggerExit(Collider other)
  80. {
  81. for (int i = 0; i < trigGroundList.Count; i++)
  82. {
  83. if (trigGroundList[i] == other.gameObject)
  84. {
  85. trigGroundList.RemoveAt(i);
  86. i--;
  87. }
  88. }
  89. }
  90. }