HexagonTrigger.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace Pathfinding.Examples {
  5. /// <summary>Helper script in the example scene 'Turn Based'</summary>
  6. [RequireComponent(typeof(Animator))]
  7. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_hexagon_trigger.php")]
  8. public class HexagonTrigger : MonoBehaviour {
  9. public Button button;
  10. Animator anim;
  11. bool visible;
  12. void Awake () {
  13. anim = GetComponent<Animator>();
  14. button.interactable = false;
  15. }
  16. void OnTriggerEnter (Collider coll) {
  17. var unit = coll.GetComponentInParent<TurnBasedAI>();
  18. var node = AstarPath.active.GetNearest(transform.position).node;
  19. // Check if it was a unit and the unit was headed for this node
  20. if (unit != null && unit.targetNode == node) {
  21. button.interactable = true;
  22. visible = true;
  23. anim.CrossFade("show", 0.1f);
  24. }
  25. }
  26. void OnTriggerExit (Collider coll) {
  27. if (coll.GetComponentInParent<TurnBasedAI>() != null && visible) {
  28. button.interactable = false;
  29. anim.CrossFade("hide", 0.1f);
  30. }
  31. }
  32. }
  33. }