SlopeCollider.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using UnityEngine;
  3. public enum RotateDirection
  4. {
  5. Unknown,
  6. None,
  7. Forward,
  8. Backward
  9. }
  10. internal struct SlopeHit
  11. {
  12. internal float distance;
  13. internal bool even;
  14. internal float angle;
  15. internal Vector3 hitPoint;
  16. internal SlopeHit(float distance, bool even, float angle, Vector3 hitPoint)
  17. {
  18. this.distance = distance;
  19. this.even = even;
  20. this.angle = angle;
  21. this.hitPoint = hitPoint;
  22. }
  23. public override string ToString()
  24. {
  25. return $"SlopeHit(distance = {distance}, even = {even}, angle = {angle})";
  26. }
  27. }
  28. public class SlopeCollider : MonoBehaviour
  29. {
  30. public int collisionLayer = 1;
  31. public Transform rearWheelContactPoint;
  32. public Transform frontWheelContactPoint;
  33. public Transform bike;
  34. private const float THRESHOLD = 0f;
  35. private float distRwFw;
  36. private RotateDirection currentRotateDirection = RotateDirection.Unknown;
  37. private void Start()
  38. {
  39. distRwFw = Mathf.Abs(frontWheelContactPoint.localPosition.z - rearWheelContactPoint.localPosition.z);
  40. }
  41. private void FixedUpdate()
  42. {
  43. var fwContact = frontWheelContactPoint.position;
  44. var rwContact = rearWheelContactPoint.position;
  45. var fwHit = DrawRay(fwContact);
  46. var rwHit = DrawRay(rwContact);
  47. //if ((fwHit?.even ?? true) && (rwHit?.even ?? true)) return;
  48. var distFw = fwHit?.distance ?? 0f;
  49. var distRw = rwHit?.distance ?? 0f;
  50. var distDif = distFw - distRw;
  51. if (fwHit.HasValue && rwHit.HasValue)
  52. {
  53. var fw = fwHit.Value;
  54. var rw = rwHit.Value;
  55. if (!fw.even && rw.even)
  56. {
  57. if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Forward)
  58. {
  59. currentRotateDirection = RotateDirection.Backward;
  60. }else if (currentRotateDirection != RotateDirection.Backward)
  61. {
  62. currentRotateDirection = RotateDirection.Forward;
  63. }
  64. }else if (fw.even && !rw.even)
  65. {
  66. if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Backward)
  67. {
  68. currentRotateDirection = RotateDirection.Forward;
  69. }
  70. else if(currentRotateDirection != RotateDirection.Forward)
  71. {
  72. currentRotateDirection = RotateDirection.Backward;
  73. }
  74. }
  75. else
  76. {
  77. currentRotateDirection = RotateDirection.None; //TODO: maybe not
  78. }
  79. }
  80. else
  81. {
  82. if (distDif < -THRESHOLD && currentRotateDirection != RotateDirection.Forward)
  83. {
  84. currentRotateDirection = RotateDirection.Backward;
  85. }else if (distDif > THRESHOLD && currentRotateDirection != RotateDirection.Backward)
  86. {
  87. currentRotateDirection = RotateDirection.Forward;
  88. }
  89. else
  90. {
  91. currentRotateDirection = RotateDirection.None;
  92. }
  93. }
  94. Debug.Log("CurrentRotateDirection = " + currentRotateDirection);
  95. Debug.Log("----Slope Collider----");
  96. Debug.Log($"\tfwHit: {fwHit}");
  97. Debug.Log($"\trwHit: {rwHit}");
  98. if (currentRotateDirection == RotateDirection.Backward) //rwDist > fwDist
  99. {
  100. //rear wheel in the air -> rotate around front wheel -> make it go uphill
  101. var angle = Mathf.Atan(distRw / distRwFw) * Mathf.Rad2Deg;
  102. bike.RotateAround(fwContact, frontWheelContactPoint.right, -angle);
  103. }
  104. else if (currentRotateDirection == RotateDirection.Forward)
  105. {
  106. //front wheel in the air -> rotate around rear wheel
  107. var angle = Mathf.Atan(distFw / distRwFw) * Mathf.Rad2Deg;
  108. bike.RotateAround(rwContact, rearWheelContactPoint.right, angle);
  109. }
  110. /*if (rwHit.HasValue)
  111. {
  112. var rwHitVal = rwHit.Value;
  113. if (rwHitVal.angle > 0.01f)
  114. {
  115. t.RotateAround(fwContact, frontWheelContactPoint.right, -rwHitVal.angle);
  116. }
  117. /*if (rwHitVal.even)
  118. {
  119. //begin of slope
  120. Rotate(rwHitVal.distance, rwHitVal.distance - (fwHit?.distance ?? 0f), fwContact, rwContact);
  121. }
  122. }
  123. if (fwHit.HasValue)
  124. {
  125. var fwHitVal = fwHit.Value;
  126. if (fwHitVal.angle > 0.01f)
  127. {
  128. t.RotateAround(rwContact, rearWheelContactPoint.right, -fwHitVal.angle);
  129. }
  130. /*if (fwHitVal.even)
  131. {
  132. //end of slope
  133. Rotate(fwHitVal.distance, fwHitVal.distance - (rwHit?.distance ?? 0f), fwContact, rwContact);
  134. }
  135. }*/
  136. }
  137. private SlopeHit? DrawRay(Vector3 start)
  138. {
  139. var layerMask = 1 << collisionLayer;
  140. RaycastHit hit;
  141. // Does the ray intersect any objects excluding the player layer
  142. if (Physics.Raycast(start, -bike.up, out hit, 20f, layerMask))
  143. {
  144. Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
  145. //Debug.DrawRay(hit.point, hit.normal, Color.blue);
  146. var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
  147. var first = -(-bike.up * hit.distance);
  148. var second = (hit.normal);
  149. var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
  150. //Debug.Log("Dot Product: " + Vector3.Dot(first, second));
  151. //Debug.Log("Angle: " + angle);
  152. return new SlopeHit(hit.distance, !isUneven, angle, hit.point);
  153. }
  154. if (Physics.Raycast(start, bike.up, out hit, 20f, layerMask))
  155. {
  156. Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
  157. //Debug.DrawRay(hit.point, hit.normal, Color.blue);
  158. var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
  159. var first = -(-bike.up * hit.distance);
  160. var second = (hit.normal);
  161. var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
  162. //Debug.Log("Dot Product: " + Vector3.Dot(first, second));
  163. //Debug.Log("Angle: " + angle);
  164. return new SlopeHit(-hit.distance, !isUneven, angle, hit.point);
  165. }
  166. //Debug.DrawRay(start, -t.up * 20f, Color.red);
  167. return null;
  168. }
  169. }