Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Seeker : MonoBehaviour
  5. {
  6.     public List<NodeElement> path = new List<NodeElement>();
  7.     public int cordsX, cordsY;
  8.     Transform tr;
  9.     void Start()
  10.     {
  11.         tr = transform;
  12.         cordsX = Mathf.RoundToInt(tr.position.x);
  13.         cordsY = Mathf.RoundToInt(tr.position.z);
  14.     }
  15.     public Animator anim;
  16.     void LateUpdate()
  17.     {
  18.         if (path.Count > 1)
  19.         {
  20.             for (int i = 0; i < path.Count - 1; i++)
  21.             {
  22.                 Debug.DrawLine(path[i].item1, path[i + 1].item1, Color.blue);
  23.             }
  24.             transform.position = Vector3.Lerp(transform.position, path[1].item1, Time.deltaTime * 4);
  25.             if (Vector3.Distance(transform.position, path[1].item1) < 0.4f)
  26.             {
  27.                 //path.RemoveAt(0);
  28.                 //stops units if their target is occupied. doesn't work perfectly
  29.                 NodeElement path0 = path[0];
  30.                 path.RemoveAt(0);
  31.                 if (path.Count > 1)
  32.                 {
  33.                     path0.occupied = null;
  34.                     if (path[1].occupied != null && path[1].occupied != this)
  35.                     {
  36.                         GeneratePathTo(path[path.Count - 1].item1);
  37.                     }
  38.                     if (path.Count > 1)
  39.                         path[1].occupied = this;
  40.                 }
  41.                 if (path.Count > 0)
  42.                 {
  43.                     cordsX = Mathf.RoundToInt(path[0].item1.x);
  44.                     cordsY = Mathf.RoundToInt(path[0].item1.z);
  45.                 }
  46.             }
  47.  
  48.             if (path.Count > 2)
  49.             {
  50.                 transform.rotation = Quaternion.LookRotation(path[1].item1 - transform.position);
  51.                 if (anim != null)
  52.                 {
  53.                     anim.SetFloat("Speed", 1);
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 anim.SetFloat("Speed", 0);
  59.             }
  60.         }
  61.         if (Input.GetMouseButtonDown(0))
  62.         {
  63.             RaycastHit hit;
  64.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  65.             if (Physics.Raycast(ray, out hit))
  66.             {
  67.                 GeneratePathTo(hit.point);
  68.             }
  69.         }
  70.         /*if (Input.GetMouseButtonDown(1))
  71.         {
  72.             RaycastHit hit;
  73.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  74.             if (Physics.Raycast(ray, out hit))
  75.             {
  76.                 if (PathfindingMap.Instance.nodeTypes.Length >= 2)
  77.                 {
  78.                     NodeTemplate temp = PathfindingMap.Instance.nodeTypes[1];
  79.                     Vector3 pos = new Vector3(Mathf.RoundToInt(hit.point.x), 0, Mathf.RoundToInt(hit.point.z));
  80.                     Node node = PathfindingMap.VecToNode(pos);
  81.                     node.inWorld.GetComponent<MeshRenderer>().material = temp.mat;
  82.                     node.cost = temp.Cost;
  83.                     node.enterable = temp.enterable;
  84.                     PathfindingMap.Instance.grid.walls.Add(node.cords);
  85.                 }
  86.             }
  87.         }*/
  88.     }
  89.     public void GeneratePathTo(Vector3 _pos)
  90.     {
  91.         int x = Mathf.RoundToInt(_pos.x);
  92.         int y = Mathf.RoundToInt(_pos.z);
  93.         if (PathfindingMap.Instance.grid.InBounds(x, y))
  94.         {
  95.             Vector3 pos = new Vector3(x, 0, y);
  96.             NodeElement start = PathfindingMap.Instance.grid.FindNode(cordsX, cordsY);
  97.             NodeElement goal = PathfindingMap.Instance.grid.FindNode(x, y);
  98.             if (PathfindingMap.Instance.grid.Passable(goal) && goal.occupied == null)
  99.             {
  100.                 AStarSearch search = new AStarSearch(PathfindingMap.Instance.grid, start, goal);
  101.                 path = search.FindShortestPath();
  102.             }
  103.             else
  104.             {
  105.                 path = new List<NodeElement>();
  106.                 path.Add(start);
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement