A* Pathfinding Project

Creating path using only connections


#1

Hello,

Just started using your A* pathfinding project. I decided to test this plugin by making a game similar to Tanks.(https://store.steampowered.com/app/866420/TANKS/)

Here is how the scene is setup (based it on Example15_2D scene( changed node connections to 4 instead of 8))

Gameplay
The red circle will be controlled by the player using directional keys.So for example if player presses right arrow, the circle will continue to move right till it hits a wall or player pressed left arrow. Circle will only move on path made using connections between the nodes.

Problem
Here is the gameplay recording.
gameplay

As you can see when player hits left arrow, Circle starts moving left in straight line but the when pressed up arrow, the generated path is not straight causing circle to behind the wall tiles.

I am using provided Seeker and AI Path scripts to move the circle. and following lines to create new path when input is received.

 GraphHitInfo hitInfo = new GraphHitInfo();
            
            if (gridGraph.Linecast(transform.position, transform.position + (Vector3.right * 25), null, out hitInfo)) {
                seeker.StartPath(ABPath.Construct(transform.position,(Vector3)hitInfo.node.position, OnPathEvent));
            }

Looking forward to hearing from you.
Thanks


#2

Hi

If you want the unit to only move along connections I would recommend that you use the AILerp script instead which does exactly that.
Configure it like described in the section “Recommended setup for movement along connections” here: https://arongranberg.com/astar/docs/ailerp.html

Furthermore with the built-in movement scripts I would recommend that you use the ai.destination property instead of starting your own paths unless you really need to.

ai.destination = (Vector3)hitInfo.node.position;
ai.SearchPath(); // Optionally force the agent to recalculate the path immediately, don't do this if you set the destination every frame though

#3

Wow… Thank you so much for such a prompt reply and suggestions.
Game now behaves exactly the way I intended.


#4

Great that it works well for you now! :slight_smile:

If you want to support the development of the package even more, a rating and/or a review in the Asset Store goes a long way :slight_smile:


#5

Will do.
Thanks again:smile: