/* * private void FindWayToRoute() * { * if (temp != null) * RouteSetWP.RemoveRoute(temp); * * Road from = ExtractRoadFrom(WorldMap.GetTile(Position)); * WayPoint fromWP = from.WPContainer.GetClosestWP(Position); * * if (Vector3.Dot(Position + transform.right, transform.InverseTransformPoint(fromWP.Position)) < 0) * fromWP = from.WPContainer.GetClosestWPExcept(Position, fromWP); * * temp = new RouteWP(RouteCreator.CreatePathToRouteWP(prev, route, route_forward), true); * * if (temp == null) { Abort("Path does not exist"); return; } * * if (!route.Contains(temp[temp.Count - 1])) * { * Debug.Log("<color=red>WTF temp route does not lead to main route</color>\nTrying to recalculate..."); * * temp = new RouteWP(RouteCreator.CreatePathToRouteWP(fromWP, route, route_forward), true); * * if (!route.Contains(temp[temp.Count - 1])) * Debug.Log("<color=red>RECALCULATION FAILED</color>"); * } * * RouteSetWP.AddRoute(temp, false); * * on_main_route = false; * current_WP = 0; * * if (sphereCurr == null) * throw new System.NullReferenceException("sphereCurr"); * * sphereCurr.SetPosition(fromWP.Position); * * temp.HighLight(true, Color.red); * } */ private void FindWayToRoute() { List <WayPoint> newTemp = RouteCreator.CreatePathWP(current, route_forward ? route.endWP : route.startWP, false); if (newTemp == null) { Abort("Path does not exist"); return; } if (temp != null) { RouteSet.RemoveRoute(temp); } temp = new Route(newTemp, true); RouteSet.AddRoute(temp, false); on_main_route = false; current_WP = 0; sphereCurr.SetPosition(current.Position); Road curr = ExtractRoadFrom(WorldMap.GetTile(Position)); tempRoad = GetBack.GetWayToClosest(curr, mainRoad); for (int i = 0; i < tempRoad.Count; i++) { tempRoad[i].SetColor(RoadColor.Red); } //enabled = false; }
private void Move() { Route _route = on_main_route ? route : temp; if (current_WP < 0 || current_WP >= _route.Count) { throw new System.ArgumentOutOfRangeException(current_WP.ToString()); } //WayPoint toGo = current; //Vector3 WPpos = toGo.Position + v_height; WayPoint toGo = _route[current_WP]; if (toGo == null) { Debug.Log("<color=red>toGo is null</color>"); return; } Vector3 WPpos = toGo.Position + v_height; Position = Vector3.MoveTowards(this.Position, WPpos, Time.deltaTime * MoveSpeed); //var rotation = Quaternion.LookRotation(toGo.Position - this.Position); var rotation = Quaternion.LookRotation(toGo.Position - this.Position); rotation.eulerAngles = new Vector3(0, rotation.eulerAngles.y + 90f, 0); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotateSpeed); sphere.transform.position = (Position + 4 * transform.right); if (Vector3.Distance(WPpos, this.Position) <= 0.6f) { current_WP++; if (!on_main_route && current_WP >= temp.Count) { current = temp.endWP; //if (route.Contains(curr)) { } on_main_route = true; RouteSet.RemoveRoute(temp); temp.HighLight(false); temp = null; current_WP = route.IndexOf(current); //sphereCurr.SetPosition(current.Position); } if (current_WP < _route.Count) { current = _route[current_WP]; sphereCurr.SetPosition(current.Position); } if (current_WP > route.Count / 2) { route_forward = false; } } if (current_WP >= route.Count) { if (!on_main_route) { Abort("Did not reach main route"); } route_forward = true; current_WP = 0; current = route.startWP; sphereCurr.SetPosition(current.Position); } }