Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AStarMain : MonoBehaviour {
- GameObject thisObject;
- PathfindingNode[,] nodes;
- public float lookInterval;
- public int lookDistance;
- public GameObject pathMarkerObj;
- public GameObject targetObj;
- public GameObject emptyPrefab;
- public Vector2 thisPosition;
- public Vector2 targetPosition;
- // Use this for initialization
- void Start () {
- thisObject = this.gameObject;
- thisPosition = new Vector2 (lookDistance / 2, lookDistance / 2);
- nodes = new PathfindingNode[lookDistance,lookDistance];
- if ((double)(lookDistance / 2) != (int)(lookDistance / 2))
- {
- lookDistance++;
- }
- setUpGrid ();
- }
- // Update is called once per frame
- void Update () {
- RaycastHit hit = new RaycastHit();
- Physics.Raycast(this.transform.position, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1)), out hit);
- if (Input.GetMouseButtonDown(0))
- {
- targetObj.transform.position = new Vector3(hit.point.x,hit.point.y,hit.point.z);
- AStarHelper2.Calculate(nodes[(int)thisPosition.x,(int)thisPosition.y],nodes[(int)targetPosition.x,(int)targetPosition.y]);
- //Debug.Log(nodes[(int)thisPosition.x,(int)thisPosition.y].ConnectedNodes);
- }
- }
- void setUpGrid()//I create a grid of balls, if the raycast hits an object tagged with "world" it is walkable, otherwise
- //It is not.
- {
- GameObject grid;
- grid = Instantiate (emptyPrefab) as GameObject;
- grid.name = "grid";
- for (int x = 0; x < lookDistance; x++)
- {
- for (int y = 0; y < lookDistance; y++)
- {
- PathfindingNode p = ScriptableObject.CreateInstance<PathfindingNode>();
- Vector3 origin = new Vector3(x*lookInterval - lookDistance/2 + this.transform.position.x, this.transform.position.y, y*lookInterval + this.transform.position.z - lookDistance/2);
- RaycastHit hit;
- Physics.Raycast(origin, Vector3.down, out hit);
- if (hit.collider.tag == "World")
- {
- p.isWalkable = true;
- GameObject g;
- g = Instantiate(pathMarkerObj, origin, Quaternion.Euler(0,0,0)) as GameObject;
- g.transform.parent = grid.transform;
- }
- else{
- p.isWalkable = false;
- }
- nodes[x,y] = p;
- }
- }
- setUpNodeNeighbours ();
- }
- void setUpNodeNeighbours()//Where I tell which nodes are neighbours
- {
- for (int x = 0; x < lookDistance; x++)
- {
- for (int y = 0; y < lookDistance; y++)
- {
- ArrayList conNodes = new ArrayList();
- for (int a = -1; a < 1; a++)
- {
- for (int b = -1; b < 1; b++)
- {
- try{
- if(nodes[x + a, y + b].isWalkable)
- {
- conNodes.Add(nodes[x + a,y + b]);
- }
- }
- catch(System.IndexOutOfRangeException e)
- {
- }
- }
- }
- PathfindingNode[] connectedNodes = new PathfindingNode[conNodes.Count];
- for (int t = 0; t < conNodes.Count; t++)
- {
- connectedNodes[t] = conNodes[t] as PathfindingNode;
- }
- nodes[x,y].ConnectedNodes = connectedNodes;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement