- Home /
(Pathfinding) How to take a particular path, while avoiding others on a flat terrain ?
Kindly check the attached image. How do I go about making an object take a particular path from point A to point B. The path is shown in grey in the picture attached. Further questions are as follows: How do I make sure that the object doesn't fall down a pit or a cliff ? How do I make the object avoid the water ? If the object landed up on the green area, how can I make it move to the nearest grey path area and continue towards its' destination, instead of making it continue to move to the destination on the green area itself ?
Every box is a single tile with each type as a prefab. So, every green tile is 1 prefab, every path tile is 1 prefab, every tree is 1 prefab.
Kindly point me to the best resources that will aid me in achieving this behavior. In the future, I also intend to include sloped paths, but that will still be tile based. So basically, each tile will probable hold a priority that the object will check before moving over it (or not).
Thanks for taking the time to read this.
Answer by coolbird22 · May 25, 2015 at 07:20 PM
I didn't realize NavMesh Layers was now free in Unity. I used it and found it to be exactly what I required. Check it out here. Kindly note that the docs dont seem to have been updated, and still show that it is only available in the Pro version of Unity.
Answer by Megolas · May 24, 2015 at 07:49 PM
You can add a node (Just an empty transform) to each turn, then connect them (Each one holds an array of all the nodes it connects to). From here, you can use an djikstra or A* algorithm so the unit will follow the path.
It won't fall in a cliff or enter the water if not node connection passes there - just make its position lerp from one nodes position to another.
Sorry for the rushed answer, don't have much time - if you need help feel free to ask.
$$anonymous$$y coding knowledge is beginner at best, though from what I could understand from your answer, that would mean that each node would contain a fixed value of that tiles' transform, which would most probably be the center that tile. This isn't what I was hoping to make. The game object should be able to travel anywhere on the tile and find a way ahead. Also, there are scenarios when the game object is picked up and dropped on the grass tiles (green), so going by your answer, the game object will not be able to continue to move anymore, since the grass tile wont have a transform node that is inside the array. At that point, the game object should be able to locate the nearest Path tile, and continue towards its' destination while staying on the path.
Thanks for replying and I appreciate the reply.
Actually, since you usually don't travel to nodes, you just find a node that manages to raycast to the point you want. Add it to the calculation as part of the distance, and you've got yourself exactly what you wanted.
If you do end up using this method, though, add checks that you aren't going past a cliff on the way from the last node to your point. You can do that by having cliff nodes and finding line intersections between two cliff nodes and the last walking node and the target.
Again - sorry for the rushed comment.
Ok, that makes more sense. So does this method qualify to be called as a Waypoint method ? Could you kindly point to resources/tutorials that could help me achieve something like this ? Also, kindly reply at your own pace. I'm not in a hurry.
Your answer
Follow this Question
Related Questions
2D Tile Road Pathfinding 2 Answers
[C#] Find shortest path from point A to B? 2 Answers
2D Tile Based Game problem with A * Pathfinding Project 0 Answers
Pathfinding. Does not "walk" on every tile. 1 Answer
TILE MOVEMENT 0 Answers