using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class enemymovement : MonoBehaviour {
public float threshold;
int pathstep=0;
int newCurrent;
Node nearest;
Node inicio;
bool recorre;
bool cercano;
bool oneTime;
public Node[] allNodes;
private int currntNode;
private List<Node> myPath;
// Use this for initialization
void Start () {
recorre = true;
cercano = false;
nearest = allNodes [0];
oneTime = false;
}
// Update is called once per frame
void Update () {
if (recorre==true) {
transform.LookAt (allNodes[currntNode].transform);
transform.Translate (transform.forward * 5 * Time.deltaTime, Space.World);
float distance = Vector3.Distance (allNodes[currntNode].transform.position, transform.position);
if (distance < threshold) {
currntNode++;
currntNode %= allNodes.Length;
}
}
if (Input.GetMouseButtonUp(0)) {
recorre = false;
inicio = allNodes [currntNode];
float distancen;
float distancej;
for (int j = 0; j < allNodes.Length; j++) {
distancen=(Input.mousePosition - Camera.main.WorldToScreenPoint(nearest.transform.position)).magnitude;
distancej=(Input.mousePosition - Camera.main.WorldToScreenPoint(allNodes[j].transform.position)).magnitude;
if (distancej<distancen) {
nearest = allNodes [j];
newCurrent = j;
}
}
cercano = true;
}
if (cercano) {
if (!oneTime) {
findNearest ();
oneTime = true;
}
transform.LookAt (myPath [pathstep].transform);
transform.Translate (transform.forward * 5 * Time.deltaTime, Space.World);
float distance= Vector3.Distance(myPath[pathstep].transform.position,transform.position);
if (distance < threshold) {
pathstep++;
}
if (pathstep >= myPath.Count) {
cercano = false;
oneTime = false;
pathstep = 0;
recorre = true;
currntNode = newCurrent;
}
}
}
void findNearest(){
List<Node> path= Pathfinding.BreadthwiseSearch(inicio,nearest);
for (int i = 0; i < path.Count; i++) {
Debug.Log (path [i].transform.name);
}
myPath = new List<Node> (path);
}
}
No hay comentarios:
Publicar un comentario