summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/SnapToNode.cs
blob: b776bebd437e04fef4807da173615f0a2b884e4d (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
using UnityEngine;
using System.Collections;
using Pathfinding;

namespace Pathfinding.Examples {
	/// <summary>
	/// Helper editor script to snap an object to the closest node.
	/// Used in the "Turn Based" example scene for snapping obstacles to the hexagon grid.
	/// </summary>
	[ExecuteInEditMode]
	[HelpURL("https://arongranberg.com/astar/documentation/stable/snaptonode.html")]
	public class SnapToNode : MonoBehaviour {
		/// <summary>[Update]</summary>
		void Update () {
			if (transform.hasChanged && AstarPath.active != null) {
				var node = AstarPath.active.GetNearest(transform.position, NNConstraint.None).node;
				if (node != null) {
					transform.position = (Vector3)node.position;
					transform.hasChanged = false;
				}
			}
		}
		/// <summary>[Update]</summary>
	}
}