HasColliderHit.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityCharacterController
  3. {
  4. [TaskCategory("Unity/CharacterController")]
  5. [TaskDescription("Returns Success if the collider hit another object, otherwise Failure.")]
  6. public class HasColliderHit : Conditional
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. [Tooltip("The tag of the GameObject to check for a collision against")]
  11. public SharedString tag = "";
  12. [Tooltip("The object that started the collision")]
  13. public SharedGameObject collidedGameObject;
  14. private bool enteredCollision = false;
  15. public override TaskStatus OnUpdate()
  16. {
  17. return enteredCollision ? TaskStatus.Success : TaskStatus.Failure;
  18. }
  19. public override void OnEnd()
  20. {
  21. enteredCollision = false;
  22. }
  23. public override void OnControllerColliderHit(ControllerColliderHit hit)
  24. {
  25. if (string.IsNullOrEmpty(tag.Value) || tag.Value.Equals(hit.gameObject.tag)) {
  26. collidedGameObject.Value = hit.gameObject;
  27. enteredCollision = true;
  28. }
  29. }
  30. public override void OnReset()
  31. {
  32. targetGameObject = null;
  33. tag = "";
  34. collidedGameObject = null;
  35. }
  36. }
  37. }