Advertisement
Guest User

unit script

a guest
Sep 28th, 2016
89
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<Node> path = new List<Node>();
  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].cords, path[i + 1].cords, Color.blue);
  23.             }
  24.             transform.position = Vector3.Lerp(transform.position, path[1].cords, Time.deltaTime * 4);
  25.             if (Vector3.Distance(transform.position, path[1].cords) < 0.4f)
  26.             {
  27.                 //stops units if their target is occupied. doesn't work perfectly
  28.                 Node path0 = path[0];
  29.                 path.RemoveAt(0);
  30.                 if (path.Count > 1)
  31.                 {
  32.                     path0.occupied = null;
  33.                     if (path[1].occupied != null && path[1].occupied != this)
  34.                     {
  35.                         GeneratePathTo(path[path.Count - 1].cords);
  36.                     }
  37.                     if (path.Count > 1)
  38.                         path[1].occupied = this;
  39.                 }
  40.                 if (path.Count > 0)
  41.                 {
  42.                     cordsX = Mathf.RoundToInt(path[0].cords.x);
  43.                     cordsY = Mathf.RoundToInt(path[0].cords.z);
  44.                 }
  45.             }
  46.  
  47.             if (path.Count > 2)
  48.             {
  49.                 transform.rotation = Quaternion.LookRotation(path[1].cords - transform.position);
  50.                 if (anim != null)
  51.                 {
  52.                     anim.SetFloat("Speed", 1);
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 anim.SetFloat("Speed", 0);
  58.             }
  59.         }
  60.         if (Input.GetMouseButtonDown(0))
  61.         {
  62.             RaycastHit hit;
  63.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  64.             if (Physics.Raycast(ray, out hit))
  65.             {
  66.                 GeneratePathTo(hit.point);
  67.             }
  68.         }
  69.         if (Input.GetMouseButtonDown(1))
  70.         {
  71.             RaycastHit hit;
  72.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  73.             if (Physics.Raycast(ray, out hit))
  74.             {
  75.                 if (PathfindingMap.Instance.nodeTypes.Length >= 2)
  76.                 {
  77.                     NodeTemplate temp = PathfindingMap.Instance.nodeTypes[1];
  78.                     Vector3 pos = new Vector3(Mathf.RoundToInt(hit.point.x), 0, Mathf.RoundToInt(hit.point.z));
  79.                     Node node = PathfindingMap.VecToNode(pos);
  80.                     node.inWorld.GetComponent<MeshRenderer>().material = temp.mat;
  81.                     node.cost = temp.Cost;
  82.                     node.enterable = temp.enterable;
  83.                     PathfindingMap.Instance.grid.walls.Add(node.cords);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     public void GeneratePathTo(Vector3 _pos)
  89.     {
  90.         int x = Mathf.RoundToInt(_pos.x);
  91.         int y = Mathf.RoundToInt(_pos.z);
  92.         Vector3 pos = new Vector3(x, 0, y);
  93.         Node node = PathfindingMap.VecToNode(pos);
  94.         List<Vector3> _path = new List<Vector3>();
  95.         if (PathfindingMap.Instance.grid.Passable(pos) && node.occupied == null)
  96.         {
  97.             AStarSearch search = new AStarSearch(PathfindingMap.Instance.grid, new Vector3(cordsX, 0, cordsY), pos);
  98.             _path = search.FindShortestPath();
  99.             path = new List<Node>();
  100.             for (int i = 0; i < _path.Count; i++)
  101.             {
  102.                 path.Add(PathfindingMap.VecToNode(_path[i]));
  103.             }
  104.         }
  105.         else
  106.         {
  107.             path = new List<Node>();
  108.             path.Add(PathfindingMap.VecToNode(new Vector3(cordsX, 0, cordsY)));
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement