IsGrounded.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityCharacterController
  3. {
  4. [TaskCategory("Unity/CharacterController")]
  5. [TaskDescription("Returns Success if the character is grounded, otherwise Failure.")]
  6. public class IsGrounded : Conditional
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. private CharacterController characterController;
  11. private GameObject prevGameObject;
  12. public override void OnStart()
  13. {
  14. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  15. if (currentGameObject != prevGameObject) {
  16. characterController = currentGameObject.GetComponent<CharacterController>();
  17. prevGameObject = currentGameObject;
  18. }
  19. }
  20. public override TaskStatus OnUpdate()
  21. {
  22. if (characterController == null) {
  23. Debug.LogWarning("CharacterController is null");
  24. return TaskStatus.Failure;
  25. }
  26. return characterController.isGrounded ? TaskStatus.Success : TaskStatus.Failure;
  27. }
  28. public override void OnReset()
  29. {
  30. targetGameObject = null;
  31. }
  32. }
  33. }