//////////////////////////////////////////////////////////////////////////////////////////////////////////Changing direction void SetDirection(wpt.direction dir) { bool CanChange = false; foreach (var directions in CurrentWaypoint.ValidDiriections) { if (NearNextWpt()) { if (directions == dir) { CanChange = true; } } else { if (Mathf.Abs((int)direction) == Mathf.Abs((int)dir)) { CanChange = true; } } } if (CanChange) { this.direction = dir; } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////Check for valid direction bool CanGo(wpt waypoint, wpt.direction direction) { foreach (var dir in waypoint.ValidDiriections) { if (dir == direction) { return(true); } } return(false); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////Finding the Distance from one vector to another public static double distance(Vector3 CurrentVector, Vector3 FarestVector, wpt.direction dir) { switch (dir) { case wpt.direction.L: return(Mathf.Abs(FarestVector.x - CurrentVector.x)); case wpt.direction.R: return(Mathf.Abs(FarestVector.x - CurrentVector.x)); case wpt.direction.U: return(Mathf.Abs(FarestVector.y - CurrentVector.y)); case wpt.direction.D: return(Mathf.Abs(FarestVector.y - CurrentVector.y)); } return(0.0); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////Deciding the next waypoint associate to the possible direction public wpt NextWaypoint(wpt CurrentPoint, wpt.direction dir) { var CurrentPosition = CurrentPoint.transform.position; wpt Target = null; foreach (var NextWaypoint in waypoints) { var NextPosition = NextWaypoint.transform.position; switch (dir) { case wpt.direction.L: //if the next waypoint is too the left, current x > next x, current y = next y. if (NextPosition.x < CurrentPosition.x && NextPosition.y == CurrentPosition.y && NextWaypoint != CurrentPoint) { if (Target != null) { if (distance(CurrentPosition, NextPosition, wpt.direction.L) < distance(CurrentPosition, Target.transform.position, wpt.direction.L)) { if (CanGo(CurrentPoint, wpt.direction.L) && CanGo(NextWaypoint, wpt.direction.R)) { Target = NextWaypoint; } } } else { if (CanGo(CurrentPoint, wpt.direction.L) && CanGo(NextWaypoint, wpt.direction.R)) { Target = NextWaypoint; } } } break; case wpt.direction.R: //if the next waypoint is to the right, current x < next x, current y = next y. if (NextPosition.x > CurrentPosition.x && NextPosition.y == CurrentPosition.y && NextWaypoint != CurrentPoint) { if (Target != null) { if (distance(CurrentPosition, NextPosition, wpt.direction.R) < distance(CurrentPosition, Target.transform.position, wpt.direction.R)) { if (CanGo(CurrentPoint, wpt.direction.R) && CanGo(NextWaypoint, wpt.direction.L)) { Target = NextWaypoint; } } } else { if (CanGo(CurrentPoint, wpt.direction.R) && CanGo(NextWaypoint, wpt.direction.L)) { Target = NextWaypoint; } } } break; case wpt.direction.U: //if the next waypoint is up, current y < next y, current x = next x. if (NextPosition.y > CurrentPosition.y && NextPosition.x == CurrentPosition.x && NextWaypoint != CurrentPoint) { if (Target != null) { if (distance(CurrentPosition, NextPosition, wpt.direction.U) < distance(CurrentPosition, Target.transform.position, wpt.direction.U)) { if (CanGo(CurrentPoint, wpt.direction.U) && CanGo(NextWaypoint, wpt.direction.D)) { Target = NextWaypoint; } } } else { if (CanGo(CurrentPoint, wpt.direction.U) && CanGo(NextWaypoint, wpt.direction.D)) { Target = NextWaypoint; } } } break; case wpt.direction.D: //if the next waypoint is down, current y > next y, current x = next x. if (NextPosition.y < CurrentPosition.y && NextPosition.x == CurrentPosition.x && NextWaypoint != CurrentPoint) { if (Target != null) { if (distance(CurrentPosition, NextPosition, wpt.direction.D) < distance(CurrentPosition, Target.transform.position, wpt.direction.D)) { if (CanGo(CurrentPoint, wpt.direction.D) && CanGo(NextWaypoint, wpt.direction.U)) { Target = NextWaypoint; } } } else { if (CanGo(CurrentPoint, wpt.direction.D) && CanGo(NextWaypoint, wpt.direction.U)) { Target = NextWaypoint; } } } break; } } return(Target); }