SetCenter.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityCapsuleCollider
  3. {
  4. [TaskCategory("Unity/CapsuleCollider")]
  5. [TaskDescription("Sets the center of the CapsuleCollider. Returns Success.")]
  6. public class SetCenter : 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 CapsuleCollider")]
  11. public SharedVector3 center;
  12. private CapsuleCollider capsuleCollider;
  13. private GameObject prevGameObject;
  14. public override void OnStart()
  15. {
  16. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  17. if (currentGameObject != prevGameObject) {
  18. capsuleCollider = currentGameObject.GetComponent<CapsuleCollider>();
  19. prevGameObject = currentGameObject;
  20. }
  21. }
  22. public override TaskStatus OnUpdate()
  23. {
  24. if (capsuleCollider == null) {
  25. Debug.LogWarning("CapsuleCollider is null");
  26. return TaskStatus.Failure;
  27. }
  28. capsuleCollider.center = center.Value;
  29. return TaskStatus.Success;
  30. }
  31. public override void OnReset()
  32. {
  33. targetGameObject = null;
  34. center = Vector3.zero;
  35. }
  36. }
  37. }