SlopeCollider.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 SphereCollider rearWheelCollider;
  32. public SphereCollider frontWheelCollider;
  33. public Transform bike;
  34. private const float THRESHOLD = 0.003f;
  35. private float distRwFw;
  36. private RotateDirection currentRotateDirection = RotateDirection.Unknown;
  37. private Transform frontWheelContactPoint;
  38. private Transform rearWheelContactPoint;
  39. private void Start()
  40. {
  41. frontWheelContactPoint = frontWheelCollider.transform;
  42. rearWheelContactPoint = rearWheelCollider.transform;
  43. distRwFw = Mathf.Abs(frontWheelContactPoint.localPosition.z - rearWheelContactPoint.localPosition.z);
  44. }
  45. private void FixedUpdate()
  46. {
  47. var fwContact = frontWheelContactPoint.position;
  48. var rwContact = rearWheelContactPoint.position;
  49. var fwHit = DrawRay(fwContact);
  50. var rwHit = DrawRay(rwContact);
  51. if (!fwHit.HasValue || !rwHit.HasValue || rwHit.Value.even && fwHit.Value.even)
  52. {
  53. return;
  54. }
  55. Debug.Log("----Slope Collider----");
  56. Debug.Log($"\tfwHit: {fwHit}");
  57. Debug.Log($"\trwHit: {rwHit}");
  58. var fw = fwHit.Value;
  59. var rw = rwHit.Value;
  60. if (fw.distance >= THRESHOLD)
  61. {
  62. var angle = Mathf.Atan(fw.distance / distRwFw) * Mathf.Rad2Deg;
  63. Debug.Log($"Rotating {angle} deg");
  64. bike.RotateAround(rearWheelContactPoint.position, rearWheelContactPoint.right, angle);
  65. }else if (rw.distance >= THRESHOLD)
  66. {
  67. var angle = Mathf.Atan(rw.distance / distRwFw) * Mathf.Rad2Deg;
  68. bike.RotateAround(frontWheelContactPoint.position, frontWheelContactPoint.right, -angle);
  69. Debug.Log($"Rotating {-angle} deg");
  70. }
  71. //if ((fwHit?.even ?? true) && (rwHit?.even ?? true)) return;
  72. /*var distFw = fwHit?.distance ?? 0f;
  73. var distRw = rwHit?.distance ?? 0f;
  74. var distDif = distFw - distRw;
  75. if (fwHit.HasValue && rwHit.HasValue)
  76. {
  77. var fw = fwHit.Value;
  78. var rw = rwHit.Value;
  79. if (!fw.even && rw.even)
  80. {
  81. if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Forward)
  82. {
  83. currentRotateDirection = RotateDirection.Backward;
  84. }else if (currentRotateDirection != RotateDirection.Backward)
  85. {
  86. currentRotateDirection = RotateDirection.Forward;
  87. }
  88. }else if (fw.even && !rw.even)
  89. {
  90. if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Backward)
  91. {
  92. currentRotateDirection = RotateDirection.Forward;
  93. }
  94. else if(currentRotateDirection != RotateDirection.Forward)
  95. {
  96. currentRotateDirection = RotateDirection.Backward;
  97. }
  98. }
  99. else
  100. {
  101. currentRotateDirection = RotateDirection.None; //TODO: maybe not
  102. }
  103. }
  104. else
  105. {
  106. if (distDif < -THRESHOLD && currentRotateDirection != RotateDirection.Forward)
  107. {
  108. currentRotateDirection = RotateDirection.Backward;
  109. }else if (distDif > THRESHOLD && currentRotateDirection != RotateDirection.Backward)
  110. {
  111. currentRotateDirection = RotateDirection.Forward;
  112. }
  113. else
  114. {
  115. currentRotateDirection = RotateDirection.None;
  116. }
  117. }
  118. Debug.Log("CurrentRotateDirection = " + currentRotateDirection);
  119. Debug.Log("----Slope Collider----");
  120. Debug.Log($"\tfwHit: {fwHit}");
  121. Debug.Log($"\trwHit: {rwHit}");
  122. if (currentRotateDirection == RotateDirection.Backward) //rwDist > fwDist
  123. {
  124. //rear wheel in the air -> rotate around front wheel -> make it go uphill
  125. var angle = Mathf.Atan(distRw / distRwFw) * Mathf.Rad2Deg;
  126. bike.RotateAround(fwContact, frontWheelContactPoint.right, -angle);
  127. }
  128. else if (currentRotateDirection == RotateDirection.Forward)
  129. {
  130. //front wheel in the air -> rotate around rear wheel
  131. var angle = Mathf.Atan(distFw / distRwFw) * Mathf.Rad2Deg;
  132. bike.RotateAround(rwContact, rearWheelContactPoint.right, angle);
  133. }
  134. /*if (rwHit.HasValue)
  135. {
  136. var rwHitVal = rwHit.Value;
  137. if (rwHitVal.angle > 0.01f)
  138. {
  139. t.RotateAround(fwContact, frontWheelContactPoint.right, -rwHitVal.angle);
  140. }
  141. /*if (rwHitVal.even)
  142. {
  143. //begin of slope
  144. Rotate(rwHitVal.distance, rwHitVal.distance - (fwHit?.distance ?? 0f), fwContact, rwContact);
  145. }
  146. }
  147. if (fwHit.HasValue)
  148. {
  149. var fwHitVal = fwHit.Value;
  150. if (fwHitVal.angle > 0.01f)
  151. {
  152. t.RotateAround(rwContact, rearWheelContactPoint.right, -fwHitVal.angle);
  153. }
  154. /*if (fwHitVal.even)
  155. {
  156. //end of slope
  157. Rotate(fwHitVal.distance, fwHitVal.distance - (rwHit?.distance ?? 0f), fwContact, rwContact);
  158. }
  159. }*/
  160. }
  161. private SlopeHit? DrawRay(Vector3 start)
  162. {
  163. var layerMask = 1 << collisionLayer;
  164. RaycastHit hit;
  165. // Does the ray intersect any objects excluding the player layer
  166. if (Physics.Raycast(start, -bike.up, out hit, 20f, layerMask))
  167. {
  168. Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
  169. //Debug.DrawRay(hit.point, hit.normal, Color.blue);
  170. var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
  171. var first = -(-bike.up * hit.distance);
  172. var second = (hit.normal);
  173. var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
  174. //Debug.Log("Dot Product: " + Vector3.Dot(first, second));
  175. //Debug.Log("Angle: " + angle);
  176. return new SlopeHit(hit.distance, !isUneven, angle, hit.point);
  177. }
  178. if (Physics.Raycast(start, bike.up, out hit, 20f, layerMask))
  179. {
  180. Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
  181. //Debug.DrawRay(hit.point, hit.normal, Color.blue);
  182. var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
  183. var first = -(-bike.up * hit.distance);
  184. var second = (hit.normal);
  185. var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
  186. //Debug.Log("Dot Product: " + Vector3.Dot(first, second));
  187. //Debug.Log("Angle: " + angle);
  188. return new SlopeHit(-hit.distance, !isUneven, angle, hit.point);
  189. }
  190. //Debug.DrawRay(start, -t.up * 20f, Color.red);
  191. return null;
  192. }
  193. }