GetCenter.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnitySphereCollider
  3. {
  4. [TaskCategory("Unity/SphereCollider")]
  5. [TaskDescription("Stores the center of the SphereCollider. Returns Success.")]
  6. public class GetCenter : Action
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. [Tooltip("The center of the SphereCollider")]
  11. [RequiredField]
  12. public SharedVector3 storeValue;
  13. private SphereCollider sphereCollider;
  14. private GameObject prevGameObject;
  15. public override void OnStart()
  16. {
  17. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  18. if (currentGameObject != prevGameObject) {
  19. sphereCollider = currentGameObject.GetComponent<SphereCollider>();
  20. prevGameObject = currentGameObject;
  21. }
  22. }
  23. public override TaskStatus OnUpdate()
  24. {
  25. if (sphereCollider == null) {
  26. Debug.LogWarning("SphereCollider is null");
  27. return TaskStatus.Failure;
  28. }
  29. storeValue.Value = sphereCollider.center;
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. storeValue = Vector3.zero;
  35. }
  36. }
  37. }