IsSleeping.cs 1.2 KB

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