DynamicBonePlaneCollider.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using UnityEngine;
  2. [AddComponentMenu("Dynamic Bone/Dynamic Bone Plane Collider")]
  3. public class DynamicBonePlaneCollider : DynamicBoneColliderBase
  4. {
  5. // prepare data
  6. Plane m_Plane;
  7. void OnValidate()
  8. {
  9. }
  10. public override void Prepare()
  11. {
  12. Vector3 normal = Vector3.up;
  13. switch (m_Direction)
  14. {
  15. case Direction.X:
  16. normal = transform.right;
  17. break;
  18. case Direction.Y:
  19. normal = transform.up;
  20. break;
  21. case Direction.Z:
  22. normal = transform.forward;
  23. break;
  24. }
  25. Vector3 p = transform.TransformPoint(m_Center);
  26. m_Plane.SetNormalAndPosition(normal, p);
  27. }
  28. public override bool Collide(ref Vector3 particlePosition, float particleRadius)
  29. {
  30. float d = m_Plane.GetDistanceToPoint(particlePosition);
  31. if (m_Bound == Bound.Outside)
  32. {
  33. if (d < 0)
  34. {
  35. particlePosition -= m_Plane.normal * d;
  36. return true;
  37. }
  38. }
  39. else
  40. {
  41. if (d > 0)
  42. {
  43. particlePosition -= m_Plane.normal * d;
  44. return true;
  45. }
  46. }
  47. /* // consider particleRadius
  48. if (m_Bound == Bound.Outside)
  49. {
  50. if (d < particleRadius)
  51. {
  52. particlePosition += m_Plane.normal * (particleRadius - d);
  53. return true;
  54. }
  55. }
  56. else
  57. {
  58. if (d > -particleRadius)
  59. {
  60. particlePosition -= m_Plane.normal * (particleRadius + d);
  61. return true;
  62. }
  63. }
  64. */
  65. return false;
  66. }
  67. void OnDrawGizmosSelected()
  68. {
  69. if (!enabled)
  70. return;
  71. Prepare();
  72. if (m_Bound == Bound.Outside)
  73. {
  74. Gizmos.color = Color.yellow;
  75. }
  76. else
  77. {
  78. Gizmos.color = Color.magenta;
  79. }
  80. Vector3 p = transform.TransformPoint(m_Center);
  81. Gizmos.DrawLine(p, p + m_Plane.normal);
  82. }
  83. }