void Update() { if (_pathTarget != _target.position) { _pathTarget = _target.position; _path = _pathfinder.FindPath(transform.position, _pathTarget, GetComponent <CircleCollider2D>()); } }
/* * ==== DESCRIPTION ==== * * A Navigator instance is used by any object that wishes to use pathfinding. * The Navigator acts as a mediator between Tilemaps in a scene and pathfinding algorithms, * and makes extensive use of algorithms in the static MapConverter class to perform conversions. * * The Navigator is used by calling "GetWorldPath". * This provides a suggested path, each entry being a NodeGrid node's world coordinates. * Objects trying to navigate should pass in their current location and their destination, * then attempt to *directly* move to each node in turn (likely moving on to the next node * once arriving within a certain distance of the current node). * Calls should be made roughly every time an object's target destination changes. * This may be very often if chasing a moving object, so effort should be made to limit calls. * * The Navigator is handy because its inputs and outputs are world coordinates, so objects * have an easy time communicating with it. * * The Navigator also generates and stores all important pathfinding data (PathMaps and their contents), * so that each object/actor using pathfinding does not need its own copy, which saves memory. * */ /// <summary> /// Get a world location to walk towards, in order to reach the desired destination. /// </summary> /// <param name="pointA">Entity's current world location.</param></param> /// <param name="PointB">World location of final destination.</param> public Vector3[] GetWorldPath(BlockingType bType, Vector3 pointA, Vector3 pointB) { PathMap pMap = pathMapDict[bType]; Vector3Int pA_vec = MapConverter.WorldToNode(pMap, pointA); Vector3Int pB_vec = MapConverter.WorldToNode(pMap, pointB); Point pA = new Point(pA_vec.x, pA_vec.y); Point pB = new Point(pB_vec.x, pB_vec.y); List <Point> pointList = Pathfinder.FindPath(pMap.nodeGrid, pA, pB); Vector3[] coordsList = new Vector3[pointList.Count]; int i = 0; pointList.ForEach(delegate(Point p){ coordsList[i++] = MapConverter.NodeToWorld(pMap, p.x, p.y); }); return(coordsList); }