Godot Version
4.2.1
Question
I want add holes on my NavigationRegion2D, everytime i put one building on the map i want make a hole on the building location so units will avoid the tower on pathfind, i tryd but i have no results
Here i setup the Navigation Area
public virtual void BuildPathfindArea(){
GD.Print(bounds.ToString());
int cellWidth = Startup.cellSize/2;
Vector2[] points = new Vector2[4];
points[0] = new Vector2(bounds.minX,bounds.minY);
points[1] = new Vector2(bounds.maxX,bounds.minY);
points[2] = new Vector2(bounds.maxX,bounds.maxY);
points[3] = new Vector2(bounds.minX,bounds.maxY);
for (int i = 0; i < points.Length;i++){
Vector2 point = points[i];
if (i == 0) {
points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(0,-cellWidth/2);
}else if (i == 1) {
//points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(-cellWidth,0);
points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(cellWidth,0);
}else if (i == 2){
points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(0,cellWidth/2);
}else{
//points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(cellWidth,0);
points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(-cellWidth,0);
}
}
navigationPolygon = pathfindRegion.NavigationPolygon;
navigationMesh = new NavigationMeshSourceGeometryData2D();
navigationPolygon.AgentRadius = 0;
NavigationServer2D.ParseSourceGeometryData(navigationPolygon,navigationMesh,this);
navigationMesh.AddTraversableOutline(points);
NavigationServer2D.BakeFromSourceGeometryData(navigationPolygon,navigationMesh);
Rid rid = pathfindRegion.GetRid();
NavigationServer2D.RegionSetNavigationPolygon(rid, navigationPolygon);
NavigationServer2D.RegionSetEnabled(rid, true);
NavigationServer2D.RegionSetMap(rid, GetWorld2D().NavigationMap);
}
Here i try add the hole when one building is placed
public virtual void SetuptObstacle(){
Polygon2D polygon2D = new Polygon2D();
float bodyWidth = Startup.cellSize / 4 * bodySize;
float bodyHeight = bodyWidth / 2;
List<Vector2> points = new List<Vector2>();
for (float width = 0f; width < 2*Mathf.Pi; width+=2*Mathf.Pi/bodyWidth){
Vector2 point = new Vector2(bodyWidth*Mathf.Cos(width),bodyHeight*Mathf.Sin(width));
points.Add(point);
}
polygon2D.Position = Position;
polygon2D.Polygon = points.ToArray();
world.PathfindRegion.AddChild(polygon2D);
NavigationServer2D.BakeFromSourceGeometryData(world.NavigationPolygon,world.NavigationMesh);
}
Here said i could use Polygons2d Using navigation meshes — Godot Engine (latest) documentation in English that is what i did, but dont work, the Polygon is on the correctly spot / shape but still doenst work, let me show a video: