I want to have behaviour that when I place my defence kit enemies will start walking around it and when the defence kit dies (OnDestroy) enemies will be free to walk over it.
More or less my code looks like this:
Code to make graph unwalkable:
public override void PlaceKit()
{
base.PlaceKit();
var colliders= GetComponentsInChildren<Collider2D>();
foreach (var c in colliders)
{
if(c.isTrigger) continue;
var guo = new GraphUpdateObject(c.bounds) {updatePhysics = true};
AstarPath.active.UpdateGraphs(guo);
}
// for test destroy this object after 3 secs
Destroy(gameObject, 3);
}
Code to allow walk on the graph:
private void OnDestroy()
{
var colliders= GetComponentsInChildren<Collider2D>();
var bounds = new List<Bounds>(colliders.Length);
foreach (var c in colliders)
{
if(c.isTrigger) continue;
bounds.Add(c.bounds);
}
// this will start coroutine that will not be destroyed with this object
CoroutineGod.StartGodsCoroutine(AllowToWalkOnDeadTurret(bounds));
}
private IEnumerator AllowToWalkOnDeadTurret(IEnumerable<Bounds> bounds)
{
yield return new WaitForSeconds(.5f);
foreach (var bound in bounds)
{
var guo = new GraphUpdateObject(bound) {updatePhysics = true};
AstarPath.active.UpdateGraphs(guo);
}
yield return null;
}
The problem is after the object is destroyed not whole graph is made walkable again:
My turret has 2 colliders: bigger - trigger collider and smaller not trigger. if I place my turrets near by (there are touching with trigger collider, but not with normal colliders) graph on the first turret will not be updated. It looks like AstarPath.active.UpdateGraphs(guo); do not want make graph walkable if it is under trigger collider. Is this behaviour correct?
Hmm… So your collision testing -> diameter setting is 1.0 which means that only colliders inside a tile will cause a node to be unwalkable. However, your towers clearly block nodes that they do not even touch. Are you sure there are no other colliders that are affecting the graph? E.g. the player’s collider, a collider in a child object, or something else?
But wait, the tiles that are highlighted are 0.5 width and height and collider tester is 1 radius. So it means that a collider can block a tile (.5x.5) that is not touching. Am I right?
@edit: also I turned on gizmos with colliders and I cannot find any other colliders
The diameter setting is a multiplier of the node width. I know this is not super obvious in the UI, but in the beta the visualization has been greatly improved to make this clear.
So a diameter of 1.0 in the settings means a world space diameter of 0.5 in this case.
First I’m placing turret in top left corner. The turret has small circle collider (you can see green debugs line around it) and a big trigger collider.
Then I’m placing another turret that do not touch old turret, except the big trigger collider - it collides with half of the old turret.
Old turret disappears after 5 secs and after another half second it runs:
foreach (var bound in bounds)
{
var guo = new GraphUpdateObject(bound) {updatePhysics = true};
AstarPath.active.UpdateGraphs(guo);
}
As you can see the graph do not updates if trigger collider collides with it.
On the other hand new turret disappears with everything.
@edit
I turned off this big trigger collider and 2 (weird) things happened:
The case that I described you above do not happen
The graph that is not walkable is smaller - it exactly as you predicted in your previous posts.
Something is not right with trigger collider - if I turn it off or change it to normal collider everything works as intended. Can you maybe check if trigger colliders do not interfere with graph update?
Sorry for the late answer.
You are completely right, due to a bug it was detecting 2D triggers as obstacles.
I am just uploading beta version 4.3.39 which fixes this issue. You can download it here: https://www.arongranberg.com/astar/download