CapsuleCollider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. namespace BansheeEngine
  2. {
  3. /// <summary>
  4. /// Collider with capsule geometry.
  5. /// </summary>
  6. public sealed class CapsuleCollider : Collider
  7. {
  8. [SerializeField]
  9. private float radius = 0.2f;
  10. [SerializeField]
  11. private float halfHeight = 0.5f;
  12. [SerializeField]
  13. private Vector3 normal = Vector3.YAxis;
  14. /// <summary>
  15. /// Radius of the capsule.
  16. /// </summary>
  17. public float Radius
  18. {
  19. get { return radius; }
  20. set
  21. {
  22. if (radius == value)
  23. return;
  24. radius = value;
  25. if (Native != null)
  26. {
  27. Native.Radius = value;
  28. if (parent != null)
  29. parent.UpdateMassDistribution();
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// Half height of the capsule, from the origin to one of the hemispherical centers, along the normal vector.
  35. /// </summary>
  36. public float HalfHeight
  37. {
  38. get { return halfHeight; }
  39. set
  40. {
  41. if (halfHeight == value)
  42. return;
  43. halfHeight = value;
  44. if (Native != null)
  45. {
  46. Native.HalfHeight = value;
  47. if (parent != null)
  48. parent.UpdateMassDistribution();
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Position of the capsule shape, relative to the component's scene object.
  54. /// </summary>
  55. public Vector3 Center
  56. {
  57. get { return serializableData.localPosition; }
  58. set
  59. {
  60. if (serializableData.localPosition == value)
  61. return;
  62. serializableData.localPosition = value;
  63. if (Native != null)
  64. UpdateTransform();
  65. }
  66. }
  67. /// <summary>
  68. /// Normal vector of the capsule. It determines how is the capsule oriented.
  69. /// </summary>
  70. public Vector3 Normal
  71. {
  72. get { return normal; }
  73. set
  74. {
  75. if (normal == value)
  76. return;
  77. normal = value.Normalized;
  78. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  79. if (Native != null)
  80. UpdateTransform();
  81. }
  82. }
  83. /// <summary>
  84. /// Returns the native capsule collider wrapped by this component.
  85. /// </summary>
  86. private NativeCapsuleCollider Native
  87. {
  88. get { return (NativeCapsuleCollider)native; }
  89. }
  90. /// <inheritdoc/>
  91. internal override NativeCollider CreateCollider()
  92. {
  93. NativeCapsuleCollider capsuleCollider = new NativeCapsuleCollider();
  94. capsuleCollider.Radius = radius;
  95. capsuleCollider.HalfHeight = halfHeight;
  96. return capsuleCollider;
  97. }
  98. }
  99. }