Advertisement
Guest User

AStar.cs

a guest
Jan 2nd, 2015
293
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AStarPathfinding : MonoBehaviour {
  5.     public GameObject NodePrefab;
  6.     public int Height;
  7.     public int Width;
  8.     public GameObject[,] Graph;
  9.     // Use this for initialization
  10.     void Start () {
  11.         Graph = new GameObject[Width,Height];
  12.         //Generate Grid of Nodes
  13.         float posX = 0f;
  14.         float posY = 0f;
  15.         for(int i =0; i<Width;i++)
  16.         {
  17.             for(int j = 0;j<Height;)
  18.             {
  19.                 Graph[i,j] = (GameObject)Instantiate(NodePrefab,new Vector3(posX,posY),Quaternion.identity);
  20.                 Graph[i,j].transform.parent = this.transform;
  21.                 posX+=0.64f;
  22.             }
  23.             posY-=0.64f;
  24.             posX = 0;
  25.         }
  26.         //Set Neighbours
  27.  
  28.         for(int y =0; y<Width;y++)
  29.         {
  30.             for(int x = 0;x<Height;x++)
  31.             {
  32.                 Node n = Graph[y,x].GetComponent<Node>();
  33.                 //LEFT
  34.                 if(x>0)
  35.                 {
  36.                     n.AddNeighbour((Graph[y,x-1]));
  37.                 }
  38.                 //RIGHT
  39.                 if(x<Width-1)
  40.                 {
  41.                     //n.AddNeighbour(Graph[y,x+1]);
  42.                 }
  43.                 //TOP
  44.                 if(y>0)
  45.                 {
  46.                     //n.AddNeighbour(Graph[y-1,x]);
  47.                 }
  48.                 //BOT
  49.                 if(y<Height-1)
  50.                 {
  51.                     //n.AddNeighbour(Graph[y+1,x]);
  52.                 }
  53.                 //TOPLEFT
  54.  
  55.                 //TOPRIGHT
  56.  
  57.                 //BOTLEFT
  58.  
  59.                 //BOTRIGHT
  60.  
  61.             }
  62.  
  63.         }
  64.     }
  65.    
  66.     // Update is called once per frame
  67.     void Update () {
  68.  
  69.     }
  70. }
Advertisement
RAW Paste Data Copied
Advertisement