Grid graph movement for units with different size

Hello, I recently bought the pro version. I want to know if my problem can be solved with your asset. In my case I have game board(grid for example 8x10) and I have units with different size for grid(1x1, 2x2, 4x2). Can I move them on the same grid graph?

Hi

You may be interested in this page Multiple agent types - A* Pathfinding Project
Check the bottom of the page in particular.

Yes, I saw it. But I do not understand how to do this for other sizes, not only for odd ones such as, 3x3, 5x5

Can you explain to me, how I can implement that interface for other sizes like in my case(4x2, 2x2, and etc)

Hi

You can do it in the same way as is shown in the script. The only difference is that your returned path will not be at the center of the agent. The returned path always follows node centers, but there’s no node center in the middle of e.g. a 2x2 agent. So you will have to manually offset it afterward.

Note that this will not allow e.g. a 4x2 agent to rotate its representation in the graph. It will always be 4x2. Handling rotating agents while pathfinding is not something that is covered by this package.

Sorry for annoying) but how can i offset the center?

That would have to be done by you in your OnPathComplete function. Or I guess in a custom path modifier (but that’s more complex). Iterate over all points in the path.vectorPath list and add a constant offset to each of the points.

Ok, correct me if I’m wrong: I can leave the implementation as it is in the example

And then I change offset like on second screenshot?

Yes. That looks perfectly correct.
Then you just need to offset the path by nodeSize * (width/2, height/2)

Thank you, seems like it’s working
image

But what if I want the object to fit exactly on the grid squares. Something like that:
image

Again, sorry for annoying)

Right, I think my offset that I mentioned was incorrect. It should be:
nodeSize * ((width - 1)/2, (height - 1)/2)

Thank you for reply. But nothing really changes(Sorry can’t load more than 2 image, but it’s same as in a previous reply)

I show you again my code. I think problem in ITraversalProvider realization.


You are creating your SquareShape with a constant size of 2. But your _width and _height fields may not be 2. Are they also 2 or are they set to different values?

_width and _height are also set to 2. I trying to make movement for 2x2 size objects

That’s odd. In that case, the offset should be 0.5 nodes in each direction. So the object should end up between nodes, not at the center of nodes.

Yeah, I get it. But how I can do this)? If I do it like on picture, nothing changes
image
image

Hmm. That script doesn’t move the agent. How do you move the actual agent?

If you are using a movement script, you probably want to send the path to the movement script after you have modified the path:

void SearchPath() {
    ...
    // Note: use AstarPath.SearchPath here instead to avoid the movement script picking it up automatically
    AstarPath.SearchPath(path);
}

void OnPathComplete(Path path) {
    ... modify the path here
    path.originalStartPoint += offset;
    path.originalEndPoint += offset;
   
     // Apply any path modifiers
     seeker.PostProcess(path);
     // Send the path to the agent so that it can follow it
    _ai.SetPath(path);
}

My code look like that:


If I change it to that, my object doesn’t move anymore(and I don’t have SearchPath() method in AstarPath class

Sorry. AstarPath.StartPath. You should call that after you set the traversal provider.

ABPath path = ...
path.traversalProvider = ...
AstarPath.StartPath(path);

Hmmm, my code now looks like this:

But it’s still doesn’t move my object. Am I doing everything right?