Danisuper

follow path A*

Jul 12th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Collections;
  5.  
  6. public class AIEn : MonoBehaviour
  7. {
  8.  
  9.     public List<GameObject> open_list = new List<GameObject>(); //lista dei nodes che devono essere considerati per trovare il percorso
  10.     public List<GameObject> closed_list = new List<GameObject>(); //lista dei nodes giĆ  considerati
  11.     public List<GameObject> adjacent_nodes = new List<GameObject>();
  12.     public List<GameObject> path_nodes = new List<GameObject>(); //i nodes nella path finale
  13.     public GameObject target;
  14.     public GameObject my_node;
  15.     GetClosestNode targ_node;
  16.     GetClosestNode actual_node;
  17.     NodeManager node_manager;
  18.     EnemyVars en_vars;
  19.  
  20.     RaycastHit2D ray_dx;
  21.     RaycastHit2D ray_sx;
  22.     RaycastHit2D ray_up;
  23.     RaycastHit2D ray_down;
  24.  
  25.     public int current_node = 0;
  26.     float node_dist;
  27.     Vector3 dest;
  28.  
  29.     void Update()
  30.     {
  31.  
  32.         ....
  33.             //found a way
  34.             if (closed_list.Contains(targ_node.my_node))
  35.             {
  36.                 BackTracePath();
  37.                 FollowPath();
  38.             }
  39.             ....
  40.     }
  41.  
  42.     #region path follow
  43.     void FollowPath()
  44.     {
  45.         dest = GetNodePos(current_node);
  46.         node_dist = Vector2.Distance(transform.position, dest);
  47.  
  48.         if (node_dist>0.3f)
  49.         {
  50.             Debug.Log(node_dist);
  51.             transform.position = Vector3.MoveTowards(transform.position, dest, en_vars.speed * Time.deltaTime);
  52.             transform.rotation= UtilityFunctions.FaceTo(GetNodePos(current_node), transform.position);
  53.         }
  54.         else
  55.         {
  56.             ChangePathNode();
  57.         }
  58.  
  59.     }
  60.  
  61.     Vector3 GetNodePos(int n_node)
  62.     {
  63.         return path_nodes[n_node].transform.position;
  64.     }
  65.  
  66.     void ChangePathNode()
  67.     {
  68.         current_node++;
  69.         if(current_node>=path_nodes.Count)
  70.         {
  71.             current_node = 0;
  72.         }
  73.     }
  74.     #endregion
  75. }
Advertisement
Add Comment
Please, Sign In to add comment