summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/HexagonTrigger.cs
blob: 0ca166f34331fd8e9a1f05e2facce97415f69c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;

namespace Pathfinding.Examples {
	/// <summary>Helper script in the example scene 'Turn Based'</summary>
	[RequireComponent(typeof(Animator))]
	[HelpURL("https://arongranberg.com/astar/documentation/stable/hexagontrigger.html")]
	public class HexagonTrigger : MonoBehaviour {
		Animator anim;
		bool visible;

		void Awake () {
			anim = GetComponent<Animator>();
		}

		/// <summary>[OnTriggerEnter]</summary>
		void OnTriggerEnter (Collider coll) {
			var unit = coll.GetComponentInParent<TurnBasedAI>();
			var node = AstarPath.active.GetNearest(transform.position).node;

			// Check if it is an agent and the agent is headed for this node
			if (unit != null && unit.targetNode == node) {
				visible = true;
				anim.CrossFade("show", 0.1f);
			}
		}
		/// <summary>[OnTriggerEnter]</summary>

		void OnTriggerExit (Collider coll) {
			if (coll.GetComponentInParent<TurnBasedAI>() != null && visible) {
				anim.CrossFade("hide", 0.1f);
			}
		}
	}
}