SnapToNode.cs 729 B

1234567891011121314151617181920212223
  1. using UnityEngine;
  2. using System.Collections;
  3. using Pathfinding;
  4. namespace Pathfinding.Examples {
  5. /// <summary>
  6. /// Helper editor script to snap an object to the closest node.
  7. /// Used in the "Turn Based" example scene for snapping obstacles to the hexagon grid.
  8. /// </summary>
  9. [ExecuteInEditMode]
  10. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_snap_to_node.php")]
  11. public class SnapToNode : MonoBehaviour {
  12. void Update () {
  13. if (transform.hasChanged && AstarPath.active != null) {
  14. var node = AstarPath.active.GetNearest(transform.position, NNConstraint.None).node;
  15. if (node != null) {
  16. transform.position = (Vector3)node.position;
  17. transform.hasChanged = false;
  18. }
  19. }
  20. }
  21. }
  22. }