HasEnteredCollision.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks
  3. {
  4. [TaskDescription("Returns success when a collision starts. This task will only receive the physics callback if it is being reevaluated (with a conditional abort or under a parallel task).")]
  5. [TaskCategory("Physics")]
  6. public class HasEnteredCollision : Conditional
  7. {
  8. [Tooltip("The tag of the GameObject to check for a collision against")]
  9. public SharedString tag = "";
  10. [Tooltip("The object that started the collision")]
  11. public SharedGameObject collidedGameObject;
  12. private bool enteredCollision = false;
  13. public override TaskStatus OnUpdate()
  14. {
  15. return enteredCollision ? TaskStatus.Success : TaskStatus.Failure;
  16. }
  17. public override void OnEnd()
  18. {
  19. enteredCollision = false;
  20. }
  21. public override void OnCollisionEnter(Collision collision)
  22. {
  23. if (string.IsNullOrEmpty(tag.Value) || tag.Value.Equals(collision.gameObject.tag)) {
  24. collidedGameObject.Value = collision.gameObject;
  25. enteredCollision = true;
  26. }
  27. }
  28. public override void OnReset()
  29. {
  30. tag = "";
  31. collidedGameObject = null;
  32. }
  33. }
  34. }