I’m making a tower defense game. My game has a mechanism to spawn and drag towers during runtime. I use linerenderer to draw the path from start point to end point (I use ABPath.Construct). I want to know how to recalculate the path and redraw the linerender every time I drag the tower.
Do you want to recalculate the path during the drag? Or only when dropped?
If you want to do it on drop, I recommend using a collider and running it off of a collision mask layer. Then you can recalculate the collision area when you drop the item, and paths will update at the point. The nice thing about a collision area is that it will not be very taxing on the system since you can update just the zone around the tower, without needing to recalculate the ENTIRE map.
Thanks for your response, everything seems fine now. Can I ask one more question: how to check whether the path from point A to point B is blocked or not?
This is what I am using for figuring out which food objects can be gotten to by my goblins. This.transform is the goblin. It establishes node1 (start), and node 2 (the nearest vector3 transform position of a food object by pathable distance). It then checks if the path is even possible using the answer to your question: pathutilities.ispathpossible(node1, node2).
It then loops through all of the foods I have and figures out which is the closest.
for (int i = 0; i < _foodTransforms.Count; i++)
{
GraphNode node1 = AstarPath.active.GetNearest(this.transform.position, NNConstraint.Walkable).node;
Vector3 _temptransform = new Vector3(_foodTransforms[i].transform.position.x + 0.5f, _foodTransforms[i].transform.position.y + 0.5f, _foodTransforms[i].transform.position.z);
GraphNode node2 = AstarPath.active.GetNearest(_temptransform, NNConstraint.Walkable).node;
if (PathUtilities.IsPathPossible(node1, node2))
{
_distance = Vector3.Distance(this.transform.position, _temptransform);
//_distance = Vector3.Distance(this.transform.position, _foodTransforms[i].transform.position);
if (_distance < _nearestDistance)
{
_target = _foodTransforms[i];
_targetName = _foodTransforms[i].name;
_nearestDistance = _distance;
Vector2 _direction = (_target.transform.position - transform.position);
if (_target.position.x > transform.position.x) _facingRight = true; else _facingRight = false;
if (_facingRight) transform.localScale = new Vector3(-1f, 1f, 1f); else transform.localScale = new Vector3(1f, 1f, 1f);
}
}
}