SlopeCollider.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(BoxCollider))]
  4. public class SlopeCollider : MonoBehaviour
  5. {
  6. public int collisionLayer = 11;
  7. private readonly ContactPoint[] contacts = new ContactPoint[4];
  8. private Transform t;
  9. private BoxCollider bikeCollider;
  10. private void Start()
  11. {
  12. t = transform;
  13. bikeCollider = GetComponent<BoxCollider>();
  14. }
  15. private void OnCollisionStay(Collision other)
  16. {
  17. if (other.gameObject.layer == collisionLayer)
  18. {
  19. var numberOfContacts = other.GetContacts(this.contacts);
  20. //Debug.Log($"Has {numberOfContacts} contacts with {other.gameObject.name}");
  21. /*foreach (var c in contacts)
  22. {
  23. Debug.DrawLine(c.point, c.point + c.normal * 0.5f, Color.red);
  24. }*/
  25. var bounds = bikeCollider.bounds;
  26. var leftBackCorner = t.TransformPoint(bikeCollider.center - bikeCollider.size / 2);
  27. var localExtends = bikeCollider.size;
  28. localExtends.Scale(t.localScale);
  29. var rightBackCorner = leftBackCorner + t.right * localExtends.x;
  30. var forward = t.forward;
  31. var leftFrontCorner = leftBackCorner + forward * localExtends.z;
  32. var rightFrontCorner = rightBackCorner + forward * localExtends.z;
  33. var minSide = Helpers.GetMinComponent(localExtends);
  34. Debug.DrawLine(leftBackCorner, rightBackCorner, Color.red);
  35. Debug.DrawLine(leftBackCorner, leftBackCorner + t.up * localExtends.y, Color.green);
  36. Debug.DrawLine(leftBackCorner, leftFrontCorner, Color.blue);
  37. foreach(var c in contacts)
  38. {
  39. if ((leftBackCorner - c.point).magnitude < minSide/2.2f)
  40. {
  41. Debug.Log("Hit LeftBackCorner");
  42. }
  43. else if ((rightBackCorner - c.point).magnitude < minSide/2.2f)
  44. {
  45. Debug.Log("Hit RightBackCorner");
  46. }
  47. else if ((leftFrontCorner - c.point).magnitude < minSide/2.2f)
  48. {
  49. Debug.Log("Hit LeftFrontCorner");
  50. }
  51. else if ((rightFrontCorner - c.point).magnitude < minSide/2.2f)
  52. {
  53. Debug.Log("Hit RightFrontCorner");
  54. }
  55. }
  56. //Debug.Log($"Bounds = {bounds}");
  57. //Debug.Log($"LocalCenter = {localCenter}, localExtends = {localExtends}");
  58. }
  59. }
  60. }