[Solved] Check path blocking while moving an object?

Hey there,

How do I check if a path is possible while I am moving an object?

Let’s say I have a “build placement script” which is updating the building’s current position to the mouse position.

In this function I’ve added the “GraphUpdateUtilities.UpdateGraphsNoBlock” function to check is the path between the start and target node is blocked …

So, if I place a building I am calling the function “AstarPath.active.Scan();”

If I move the next building over the build grid, the position shows red. Red = Invalid Position

I am expecting a valid position here and not an invalid position.

This is how I am checking the position: https://hastebin.com/iloxumoleb.cs

I am updating the GraphUpdateObject every time when the mouse is moved.

What I am really missing here is a visualization of the path.

Thank You for your help and time =)

Hi

One thing that I notice is that the alwaysRevert parameters that you pass to the function is false. This will make the graph to stay updated the path was not blocked. Most likely you want to only actually execute the graph update when the player clicks to place the turret.

Thank you for your answer … Sadly it doesn’t changed anything.

The problem is still there.

I don’t want to check the path after the building is placed … I want to check it while the player is moving the building. I am instantiating the building if the player clicked the building in the building menu. Now, if he moves the building I am calling the GraphUpdateUtilities.UpdateGraphsNoBlock method.

https://hastebin.com/keqodidaju.cs

What I am expecting here is that the build location remains green until all places around the spawn location are blocked except one place.

The AstarPath.active.Scan(); method I am calling if the player placed the building.

Anyone an idea? How would you detect it?

Hi

Do you think you could show the graph while you are placing the building? It would be interesting to see how it updates. It would help debugging this.

Also. Make sure that the spawnLocationNode and targetLocationNode positions are walkable and that there is a valid path between them initially (you can check this using PathUtilities.IsPathPossible as well).

I’ve recorded it. On the beginning I am placing one tower … After that the path is always blocked until I’ve cancelled the placement and start the placement again.

Spawn Position = Red Light
Target Position = Cyan Light (not visible in the video)

Both places are walkable.

0:48 cancelled placement
0:50 started placement
0:57 Non blocking place found

and so on

if (GraphUpdateUtilities.UpdateGraphsNoBlock(graphUpdateObject, game.spawnLocationNode, game.targetLocationNode, true)) {
  isBlockingPath = false;
  Debug.Log("The Path is NOT blocked!");
} else {
  isBlockingPath = true;
  Debug.Log("The Path is blocked!");
}

If you want and if you have a GitLab Account, I can invite you to the project and you can debug it on your local machine.

Ah wait. You call AstarPath.Scan after you created the tower? This will recreate the whole graph from scratch, including the nodes. So if you cache the spawnLocationNode and targetLocationNode references, those would just refer to destroyed nodes after that call. This might be why everything fails after the first update.

I would recommend that you either just do a normal graph update (which does not invalidate all previous nodes) or that you recalculate the spawn/tagetLocationNode every time you need them.

I’ve updated the code. Now … The path isn’t blocked. I think I have a worm in the code :frowning:

2019-05-17%2013_34_00-Window

Here is the whole code PlacementManager.txt (5.7 KB)

Maybe I am still using the wrong UpdateMethod or I am stupid :smiley:

If you place that final turret that completely seals in the start area (or is it the end area?), does it still allow you to place more turrets?

Very good question :smiley: Don’t test it before … But no, it does not allow it. Good Point …

2019-05-17%2013_47_32-Window

Ah. I noticed something. Your tower collider seems to be disabled while you are moving it around. It needs to be enabled, otherwise the GraphUpdateUtilities.UpdateGraphsNoBlock method will not realize that anything changes. The graph update that it does will not cause any changes to the graph, so it thinks everything is ok.

It’s now working. I’ve switched to the collider of the building place. First it doesn’t work but then I’ve realized that I need to use the “Unwalkable” layer for the position.

Layer 9 = Unwalkable
Layer 10 = BuildingGrid

// Restore old layer
currentBuildLocation.gameObject.layer = 10;

// Set new build location
currentBuildLocation = hit.transform;

// Set layer to the new build location
currentBuildLocation.gameObject.layer = 9;

// Update the graph update object
UpdateGraphObject(currentBuildLocation);

// Set the isBlockingPath variable
isBlockingPath = IsPathBlocking();

Thank you very very much for your support!

2 Likes