// Returns waypoint closest to the parameter:position public Waypoint_V2 FindClosestWaypoint(Vector2 position, bool walkableOnly) { List <RaycastHit2D> hits = new List <RaycastHit2D>(); float raycastDistance = walkableOnly ? 5f : 3f; Physics2D.CircleCast(position, raycastDistance, Vector2.zero, new ContactFilter2D().NoFilter(), hits); hits.RemoveAll(x => x.transform.gameObject.layer != LayerMask.NameToLayer("waypoint")); if (walkableOnly) { hits.RemoveAll(x => !x.transform.GetComponent <Waypoint_V2>().walkable); } float smallestDistance = float.PositiveInfinity; Waypoint_V2 closestHit = null; foreach (RaycastHit2D hit in hits) { float d = Vector2.Distance(hit.transform.position, position); if (smallestDistance > d) { closestHit = hit.transform.GetComponent <Waypoint_V2>(); smallestDistance = d; } } if (closestHit != null) { return(closestHit); } else { Debug.LogWarning("Could not find closest waypoint to the following position: " + position); return(null); } }
private void OnDrawGizmos() { if (Grid_V2.Instance == null) { return; } Gizmos.color = Color.red; Waypoint_V2 toWp = Grid_V2.Instance.FindClosestWaypoint(this.transform.position, false); if (toWp != null) { Gizmos.DrawLine(this.transform.position, toWp.transform.position); } }
//Gets corner closest to posElement public GameObject getCorner(Vector2 posEstimate) { try { Waypoint_V2 closestWaypoint = Grid_V2.Instance.FindClosestWaypoint(this.transform.position, false); TacticalRegion region = closestWaypoint.transform.GetComponentInParent <TacticalRegion>(); cornerTilemap = region.gameObject; } catch (Exception e) { //Debug.Log(e); cornerTilemap = null; } return(cornerTilemap); }
//////////////////////////////////////////////////////////////////////////////////////// // Node Functions // gets the closest node to a particular point public Node GetClosestNode(Vector2 point) { Waypoint_V2 wp = Grid_V2.Instance.FindClosestWaypoint(point, true); return(new Node(wp)); }
public Node(Waypoint_V2 wp_) { this.wp = wp_; this.heuristic = 0; }