Advertisement
Guest User

AStarMain Script

a guest
Oct 25th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AStarMain : MonoBehaviour {
  5.     GameObject thisObject;
  6.     PathfindingNode[,] nodes;
  7.  
  8.     public float lookInterval;
  9.     public int lookDistance;
  10.     public GameObject pathMarkerObj;
  11.     public GameObject targetObj;
  12.     public GameObject emptyPrefab;
  13.     public Vector2 thisPosition;
  14.     public Vector2 targetPosition;
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.         thisObject = this.gameObject;
  19.         thisPosition = new Vector2 (lookDistance / 2, lookDistance / 2);
  20.         nodes = new PathfindingNode[lookDistance,lookDistance];
  21.         if ((double)(lookDistance / 2) != (int)(lookDistance / 2))
  22.         {
  23.             lookDistance++;
  24.         }
  25.         setUpGrid ();
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update () {
  30.         RaycastHit hit = new RaycastHit();
  31.         Physics.Raycast(this.transform.position, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1)), out hit);
  32.         if (Input.GetMouseButtonDown(0))
  33.         {
  34.             targetObj.transform.position = new Vector3(hit.point.x,hit.point.y,hit.point.z);
  35.             AStarHelper2.Calculate(nodes[(int)thisPosition.x,(int)thisPosition.y],nodes[(int)targetPosition.x,(int)targetPosition.y]);
  36.             //Debug.Log(nodes[(int)thisPosition.x,(int)thisPosition.y].ConnectedNodes);
  37.         }
  38.     }
  39.  
  40.     void setUpGrid()//I create a grid of balls, if the raycast hits an object tagged with "world" it is walkable, otherwise
  41.             //It is not.
  42.     {
  43.         GameObject grid;
  44.         grid = Instantiate (emptyPrefab) as GameObject;
  45.         grid.name = "grid";
  46.         for (int x = 0; x < lookDistance; x++)
  47.         {
  48.             for (int y = 0; y < lookDistance; y++)
  49.             {
  50.                 PathfindingNode p = ScriptableObject.CreateInstance<PathfindingNode>();
  51.                 Vector3 origin = new Vector3(x*lookInterval - lookDistance/2 + this.transform.position.x, this.transform.position.y, y*lookInterval  + this.transform.position.z - lookDistance/2);
  52.                 RaycastHit hit;
  53.                 Physics.Raycast(origin, Vector3.down, out hit);
  54.                 if (hit.collider.tag == "World")
  55.                 {
  56.                     p.isWalkable = true;
  57.                     GameObject g;
  58.                     g = Instantiate(pathMarkerObj, origin, Quaternion.Euler(0,0,0)) as GameObject;
  59.                     g.transform.parent = grid.transform;
  60.                 }
  61.                 else{
  62.                     p.isWalkable = false;
  63.                 }
  64.                 nodes[x,y] = p;
  65.             }
  66.         }
  67.         setUpNodeNeighbours ();
  68.     }
  69.  
  70.     void setUpNodeNeighbours()//Where I tell which nodes are neighbours
  71.     {
  72.         for (int x = 0; x < lookDistance; x++)
  73.         {
  74.             for (int y = 0; y < lookDistance; y++)
  75.             {
  76.                 ArrayList conNodes = new ArrayList();
  77.                 for (int a = -1; a < 1; a++)
  78.                 {
  79.                     for (int b = -1; b < 1; b++)
  80.                     {
  81.                         try{
  82.                             if(nodes[x + a, y + b].isWalkable)
  83.                             {
  84.                                 conNodes.Add(nodes[x + a,y + b]);
  85.                             }
  86.                         }
  87.                         catch(System.IndexOutOfRangeException e)
  88.                         {
  89.                         }
  90.                     }
  91.                 }
  92.                 PathfindingNode[] connectedNodes = new PathfindingNode[conNodes.Count];
  93.                 for (int t = 0; t < conNodes.Count; t++)
  94.                 {
  95.                     connectedNodes[t] = conNodes[t] as PathfindingNode;
  96.                 }
  97.                 nodes[x,y].ConnectedNodes = connectedNodes;
  98.  
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement