Circlecast.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityPhysics2D
  3. {
  4. [TaskCategory("Unity/Physics2D")]
  5. [TaskDescription("Casts a circle against all colliders in the scene. Returns success if a collider was hit.")]
  6. public class Circlecast : Action
  7. {
  8. [Tooltip("Starts the circlecast at the GameObject's position. If null the originPosition will be used.")]
  9. public SharedGameObject originGameObject;
  10. [Tooltip("Starts the circlecast at the position. Only used if originGameObject is null.")]
  11. public SharedVector2 originPosition;
  12. [Tooltip("The radius of the circlecast")]
  13. public SharedFloat radius;
  14. [Tooltip("The direction of the circlecast")]
  15. public SharedVector2 direction;
  16. [Tooltip("The length of the ray. Set to -1 for infinity.")]
  17. public SharedFloat distance = -1;
  18. [Tooltip("Selectively ignore colliders.")]
  19. public LayerMask layerMask = -1;
  20. [Tooltip("Use world or local space. The direction is in world space if no GameObject is specified.")]
  21. public Space space = Space.Self;
  22. [SharedRequired]
  23. [Tooltip("Stores the hit object of the circlecast.")]
  24. public SharedGameObject storeHitObject;
  25. [SharedRequired]
  26. [Tooltip("Stores the hit point of the circlecast.")]
  27. public SharedVector2 storeHitPoint;
  28. [SharedRequired]
  29. [Tooltip("Stores the hit normal of the circlecast.")]
  30. public SharedVector2 storeHitNormal;
  31. [SharedRequired]
  32. [Tooltip("Stores the hit distance of the circlecast.")]
  33. public SharedFloat storeHitDistance;
  34. public override TaskStatus OnUpdate()
  35. {
  36. Vector2 position;
  37. Vector2 dir = direction.Value;
  38. if (originGameObject.Value != null) {
  39. position = originGameObject.Value.transform.position;
  40. if (space == Space.Self) {
  41. dir = originGameObject.Value.transform.TransformDirection(direction.Value);
  42. }
  43. } else {
  44. position = originPosition.Value;
  45. }
  46. var hit = Physics2D.CircleCast(position, radius.Value, dir, distance.Value == -1 ? Mathf.Infinity : distance.Value, layerMask);
  47. if (hit.collider != null) {
  48. storeHitObject.Value = hit.collider.gameObject;
  49. storeHitPoint.Value = hit.point;
  50. storeHitNormal.Value = hit.normal;
  51. storeHitDistance.Value = hit.distance;
  52. return TaskStatus.Success;
  53. }
  54. return TaskStatus.Failure;
  55. }
  56. public override void OnReset()
  57. {
  58. originGameObject = null;
  59. originPosition = Vector2.zero;
  60. direction = Vector2.zero;
  61. radius = 0;
  62. distance = -1;
  63. layerMask = -1;
  64. space = Space.Self;
  65. }
  66. }
  67. }