Advertisement
Guest User

unit script

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