I couldn’t find any code example for the following problem. I really hope someone could help.
I want to let player draw a path (the black line in the example image).
Then I want to render the shortest possible route (green line).
The drawing can start and end anywhere but the route should always start from the players position (blue dot in the image) and go around the grey areas.
My current functions for drawing the line are like below (following a tutorial) but then I released my incompetence
void ResetLine ()
lineRenderer.positionCount = 0;
targetPos = 0;
void UpdateLine(Vector2 newFingerPosition)
lineRenderer.SetPosition(lineRenderer.positionCount - 1, newFingerPosition);
You could try a pathfinding algorithm. They calculate all possible routes from one point to another that don’t cross paths with any walls/obstacles and return the shortest path. You can try the A* pathfinding algorithm, from the asset store, which uses a grid to do this. Or you could implement it yourself, if you’re up for the challenge. @w-harri
Well your problem is very vague. The initial problem seems to be you want to get a player from A to B. What if there’s no colliders in the way? so you could fly / walk / move in a straight line. You usually want to do the mesh “hugging” when there’s an obstacle in the direct line of sight. So once you reach the bottom most point on the first mesh you get a direct line of sight to the target so what on earth would cause your player to take that detour you drawn? Based on what decision? What if there are 10 other colliders? Does the player have to visit all of them? As I said the initial problem is not clear at all. Also you said the shortest path. However you drawn a hugging path which naturally is longer on any concave mesh.
Finally the most important question: Is this a 2d problem or 3d problem? In 3d it becomes much more difficult and my first questions get even more degrees of freedom. So your exact wanted behaviour has to be narrowed down first. Note that getting pathfinding right is a quite tough topic.