2
0

CustomWheelCollider.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. public struct HitInfo
  5. {
  6. public readonly RaycastHit Hit;
  7. public readonly float Timestamp;
  8. public HitInfo(float timestamp, RaycastHit hit)
  9. {
  10. this.Timestamp = timestamp;
  11. this.Hit = hit;
  12. }
  13. }
  14. public class CustomWheelCollider : MonoBehaviour
  15. {
  16. public float radius;
  17. public int numberOfRays;
  18. public int collisionLayer = 11;
  19. //[Range(10f,90f)]
  20. //public float maxDetectableAngle = 45f;
  21. private Transform t;
  22. private Vector3[] rayDirections;
  23. public HitInfo? CurrentHit { private set; get; } = null;
  24. public int CurrentHitNumber { private set; get; }
  25. private void Start()
  26. {
  27. t = transform;
  28. rayDirections = CalculateRayDirections();
  29. }
  30. private void FixedUpdate()
  31. {
  32. var results = new HitInfo?[numberOfRays];
  33. for (var i = 0; i < numberOfRays; i++)
  34. {
  35. results[i] = CastRay(i);
  36. }
  37. CurrentHit = results.FirstOrDefault(r => r != null);
  38. CurrentHitNumber = results.Count(r => r != null);
  39. }
  40. public Vector3[] CalculateRayDirections()
  41. {
  42. //var startAngle = 90f - maxDetectableAngle;
  43. //var stopAngle = 90f + maxDetectableAngle;
  44. var anglePerRot = 360f / numberOfRays;
  45. var directions = new Vector3[numberOfRays];
  46. for (var i = 0; i < numberOfRays; i++)
  47. {
  48. directions[i] = CalculateRayDirection(i * anglePerRot * Mathf.Deg2Rad);
  49. }
  50. return directions;
  51. }
  52. private Vector3 CalculateRayDirection(float angle)
  53. {
  54. //(1,0,0) rotated by angle
  55. var x = 1f;
  56. var y = 0f;
  57. return new Vector3(0, Mathf.Sin(angle) * x + Mathf.Cos(angle) * y, Mathf.Cos(angle) * x - Mathf.Sin(angle) * y);
  58. }
  59. /*private Vector3 RotateOnYZAxis(Vector3 vector, float angle)
  60. {
  61. var y = vector.y;
  62. var z = vector.z;
  63. return new Vector3(0, Mathf.Sin(angle) * y + Mathf.Cos(angle) * z, Mathf.Cos(angle) * y - Mathf.Sin(angle) * z);
  64. }*/
  65. private HitInfo? CastRay(int index)
  66. {
  67. var direction = t.TransformDirection(rayDirections[index]);
  68. var layerMask = 1 << collisionLayer;
  69. RaycastHit hit;
  70. //var direction = t.forward;
  71. //direction = new Vector3(direction.x, direction.z * Mathf.Sin(angle) + direction.y * Mathf.Cos(angle),
  72. // direction.z * Mathf.Cos(angle) - direction.y * Mathf.Sin(angle));
  73. if (Physics.Raycast(t.position, direction, out hit, radius, layerMask))
  74. {
  75. Debug.DrawRay(t.position, direction * hit.distance, Color.yellow);
  76. //Debug.Log("Did Hit: ");
  77. return new HitInfo(Time.fixedTime, hit);
  78. }
  79. else
  80. {
  81. Debug.DrawRay(t.position, direction * radius, Color.white);
  82. return null;
  83. }
  84. }
  85. }