PlaneCollider.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Collider with plane geometry.
  7. /// </summary>
  8. public sealed class PlaneCollider : Collider
  9. {
  10. [SerializeField]
  11. private float distance;
  12. [SerializeField]
  13. private Vector3 normal = new Vector3(1.0f, 0.0f, 0.0f);
  14. /// <summary>
  15. /// Distance of the plane from the local origin, along its normal vector.
  16. /// </summary>
  17. public float Distance
  18. {
  19. get { return distance; }
  20. set
  21. {
  22. if (distance == value)
  23. return;
  24. distance = value;
  25. serializableData.localPosition = normal * distance;
  26. if (Native != null)
  27. UpdateTransform();
  28. }
  29. }
  30. /// <summary>
  31. /// Normal vector of the plane. It determines how is the plane oriented.
  32. /// </summary>
  33. public Vector3 Normal
  34. {
  35. get { return normal; }
  36. set
  37. {
  38. if (normal == value)
  39. return;
  40. normal = value.Normalized;
  41. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  42. if (Native != null)
  43. UpdateTransform();
  44. }
  45. }
  46. /// <inheritdoc/>
  47. protected internal override bool IsValidParent(Rigidbody parent)
  48. {
  49. // Planes cannot be added to non-kinematic rigidbodies
  50. return parent.Kinematic;
  51. }
  52. /// <summary>
  53. /// Returns the native plane collider wrapped by this component.
  54. /// </summary>
  55. private NativePlaneCollider Native
  56. {
  57. get { return (NativePlaneCollider)native; }
  58. }
  59. public PlaneCollider()
  60. {
  61. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  62. }
  63. /// <inheritdoc/>
  64. internal override NativeCollider CreateCollider()
  65. {
  66. return new NativePlaneCollider();
  67. }
  68. }
  69. }