void DriveToCurrentWaypoint() { //Waypoint reached if (WaypointReached(currentWaypoint)) { //tell plan that we have arrived at the waypoint NotifyWaypointReached(); return; } if (currentWaypoint == null) { Debug.LogError("Car driving to null."); return; } //change direction Vector3 heading = currentWaypoint.AsVector() - transform.position.ToVector2(); directionHeading = heading.normalized; //Do actual driving Accelerate(maxSpeed); Steer(); }
//Slowly move the car to waypoint w public IEnumerator MoveToWaypoint(Waypoint w) { while (!WaypointReached(w)) { var diff = w.AsVector() - transform.position.ToVector2(); var add = (diff * 0.01f).ToVector3(); transform.position += Vector3.Lerp(Vector3.zero, add, Time.deltaTime); yield return(new WaitForFixedUpdate()); } yield break; }
//Checks whether the car is at the waypoint bool WaypointReached(Waypoint w) { if (w == null) { return(false); } Vector2 dist = w.AsVector() - new Vector2(transform.position.x, transform.position.y); if (dist.magnitude < 0.1f) { if (w.onRoad == null) { Debug.Log(w); } road = w.onRoad; return(true); } return(false); }