Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Seeker : MonoBehaviour
- {
- public List<NodeElement> path = new List<NodeElement>();
- public int cordsX, cordsY;
- Transform tr;
- void Start()
- {
- tr = transform;
- cordsX = Mathf.RoundToInt(tr.position.x);
- cordsY = Mathf.RoundToInt(tr.position.z);
- }
- public Animator anim;
- void LateUpdate()
- {
- if (path.Count > 1)
- {
- for (int i = 0; i < path.Count - 1; i++)
- {
- Debug.DrawLine(path[i].item1, path[i + 1].item1, Color.blue);
- }
- transform.position = Vector3.Lerp(transform.position, path[1].item1, Time.deltaTime * 4);
- if (Vector3.Distance(transform.position, path[1].item1) < 0.4f)
- {
- //path.RemoveAt(0);
- //stops units if their target is occupied. doesn't work perfectly
- NodeElement path0 = path[0];
- path.RemoveAt(0);
- if (path.Count > 1)
- {
- path0.occupied = null;
- if (path[1].occupied != null && path[1].occupied != this)
- {
- GeneratePathTo(path[path.Count - 1].item1);
- }
- if (path.Count > 1)
- path[1].occupied = this;
- }
- if (path.Count > 0)
- {
- cordsX = Mathf.RoundToInt(path[0].item1.x);
- cordsY = Mathf.RoundToInt(path[0].item1.z);
- }
- }
- if (path.Count > 2)
- {
- transform.rotation = Quaternion.LookRotation(path[1].item1 - transform.position);
- if (anim != null)
- {
- anim.SetFloat("Speed", 1);
- }
- }
- else
- {
- anim.SetFloat("Speed", 0);
- }
- }
- if (Input.GetMouseButtonDown(0))
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- GeneratePathTo(hit.point);
- }
- }
- /*if (Input.GetMouseButtonDown(1))
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- if (PathfindingMap.Instance.nodeTypes.Length >= 2)
- {
- NodeTemplate temp = PathfindingMap.Instance.nodeTypes[1];
- Vector3 pos = new Vector3(Mathf.RoundToInt(hit.point.x), 0, Mathf.RoundToInt(hit.point.z));
- Node node = PathfindingMap.VecToNode(pos);
- node.inWorld.GetComponent<MeshRenderer>().material = temp.mat;
- node.cost = temp.Cost;
- node.enterable = temp.enterable;
- PathfindingMap.Instance.grid.walls.Add(node.cords);
- }
- }
- }*/
- }
- public void GeneratePathTo(Vector3 _pos)
- {
- int x = Mathf.RoundToInt(_pos.x);
- int y = Mathf.RoundToInt(_pos.z);
- if (PathfindingMap.Instance.grid.InBounds(x, y))
- {
- Vector3 pos = new Vector3(x, 0, y);
- NodeElement start = PathfindingMap.Instance.grid.FindNode(cordsX, cordsY);
- NodeElement goal = PathfindingMap.Instance.grid.FindNode(x, y);
- if (PathfindingMap.Instance.grid.Passable(goal) && goal.occupied == null)
- {
- AStarSearch search = new AStarSearch(PathfindingMap.Instance.grid, start, goal);
- path = search.FindShortestPath();
- }
- else
- {
- path = new List<NodeElement>();
- path.Add(start);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement