IEnumerator LerpAlongPath(Stack <Vector3> path) { traversing = true; // If we already have a cube destroy it if (cube != null) { DestroyObject(cube); } // Instantiate a cube to move cube = GameObject.CreatePrimitive(PrimitiveType.Cube); // Pop first waypoint off as the starting point Vector3 current = path.Pop(); cube.transform.position = current; while (path.Count > 0) { Vector3 target = path.Pop(); float lerpTime = voxelChunk.GetDistanceCost(target); float currentTime = 0.0f; //Lerp to out next waypoint until time has expired while (currentTime < lerpTime) { currentTime += Time.deltaTime; cube.transform.position = Vector3.Lerp(current, target, currentTime / lerpTime); yield return(0); } cube.transform.position = target; current = target; } traversing = false; }
Stack <Vector3> Dijkstra(Vector3 start, Vector3 end, VoxelChunk vc) { Stack <Vector3> waypoints = new Stack <Vector3>(); List <Vector3> l = new List <Vector3>(); disDictionary = new Dictionary <Vector3, int>(); Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3>(); bool found = false; Vector3 current = start; int newDist; disDictionary[start] = 0; if (vc.isTraversable(current)) { l.Add(current); } while (l.Count > 0 && !found) { int minDistance = disDictionary[l[0]]; current = l[0]; for (int i = 0; i < l.Count; i++) { if (minDistance > disDictionary[l[i]]) { current = l[i]; minDistance = vc.GetDistanceCost(l[i]); } } l.Remove(current); if (current != end) { List <Vector3> neighbourList = new List <Vector3>(); neighbourList.Add(current + new Vector3(1, 0, 0)); neighbourList.Add(current + new Vector3(-1, 0, 0)); neighbourList.Add(current + new Vector3(0, 0, 1)); neighbourList.Add(current + new Vector3(0, 0, -1)); foreach (Vector3 n in neighbourList) { if (n.x >= 0 && n.x < vc.GetChunkSize() && n.z >= 0 && n.z < vc.GetChunkSize() && vc.isTraversable(n)) { newDist = disDictionary[current] + vc.GetDistanceCost(n); if (!visitedParent.ContainsKey(n) || newDist < disDictionary[n]) { disDictionary[n] = newDist; visitedParent[n] = current; l.Add(n); } } } } else { found = true; } } if (found) { Debug.Log("The path cost is: " + disDictionary[current]); while (current != start) { waypoints.Push(current + offset); current = visitedParent[current]; } waypoints.Push(start + offset); } return(waypoints); }