navigation_using_navigationregions.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. .. _doc_navigation_using_navigationregions:
  2. Using NavigationRegions
  3. =======================
  4. NavigationRegions are the visual Node representation of a **region** of the navigation **map** on the NavigationServer.
  5. Each NavigationRegion node holds a resource for the navigation mesh data.
  6. Both 2D and 3D version are available as :ref:`NavigationRegion2D<class_NavigationRegion2D>`
  7. and :ref:`NavigationRegion3D<class_NavigationRegion3D>` respectively.
  8. Individual NavigationRegions upload their 2D NavigationPolygon or 3D NavigationMesh resource data to the NavigationServer.
  9. The NavigationServer map turns this information into a combined navigation map for pathfinding.
  10. To create a navigation region using the scene tree add a ``NavigationRegion2D`` or ``NavigationRegion3D`` node to the scene.
  11. All regions require a navigation mesh resource to function. See :ref:`doc_navigation_using_navigationmeshes` to learn how to create and apply navigation meshes.
  12. NavigationRegions will automatically push ``global_transform`` changes to the region on the NavigationServer which makes them suitable for moving platforms.
  13. The NavigationServer will attempt to connect the navigation meshes of individual regions when they are close enough. For more details see :ref:`doc_navigation_connecting_navmesh`.
  14. To connect NavigationRegions over arbitrary distances see :ref:`doc_navigation_using_navigationlinks` to learn how to create and use ``NavigationLinks``.
  15. .. warning::
  16. While changing the transform of a NavigationRegion node does update the region position on the
  17. NavigationServer, changing the scale does not. A navigation mesh resource has no scale and needs
  18. to be fully updated when source geometry changes scale.
  19. Regions can be enabled / disabled and if disabled will not contribute to future pathfinding queries.
  20. .. note::
  21. Existing paths will not be automatically updated when a region gets enabled / disabled.
  22. Creating new navigation regions
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. New NavigationRegion nodes will automatically register to the default world navigation map for their 2D/3D dimension.
  25. The region RID can then be obtained from NavigationRegion Nodes with ``get_rid()``.
  26. .. tabs::
  27. .. code-tab:: gdscript 2D GDScript
  28. extends NavigationRegion2D
  29. var navigationserver_region_rid: RID = get_rid()
  30. .. code-tab:: csharp 2D C#
  31. public partial class MyNavigationRegion2D : NavigationRegion2D
  32. {
  33. public override void _Ready()
  34. {
  35. Rid navigationServerRegionRid = GetRid();
  36. }
  37. }
  38. .. code-tab:: gdscript 3D GDScript
  39. extends NavigationRegion3D
  40. var navigationserver_region_rid: RID = get_rid()
  41. .. code-tab:: csharp 3D C#
  42. public partial class MyNavigationRegion3D : NavigationRegion3D
  43. {
  44. public override void _Ready()
  45. {
  46. Rid navigationServerRegionRid = GetRid();
  47. }
  48. }
  49. New regions can also be created with the NavigationServer API and added to any existing map.
  50. If regions are created with the NavigationServer API directly they need to be assigned a navigation map manually.
  51. .. tabs::
  52. .. code-tab:: gdscript 2D GDScript
  53. extends Node2D
  54. func _ready() -> void:
  55. var new_region_rid: RID = NavigationServer2D.region_create()
  56. var default_map_rid: RID = get_world_2d().get_navigation_map()
  57. NavigationServer2D.region_set_map(new_region_rid, default_map_rid)
  58. .. code-tab:: csharp 2D C#
  59. public partial class MyNode2D : Node2D
  60. {
  61. public override void _Ready()
  62. {
  63. Rid newRegionRid = NavigationServer2D.RegionCreate();
  64. Rid defaultMapRid = GetWorld2D().NavigationMap;
  65. NavigationServer2D.RegionSetMap(newRegionRid, defaultMapRid);
  66. }
  67. }
  68. .. code-tab:: gdscript 3D GDScript
  69. extends Node3D
  70. func _ready() -> void:
  71. var new_region_rid: RID = NavigationServer3D.region_create()
  72. var default_map_rid: RID = get_world_3d().get_navigation_map()
  73. NavigationServer3D.region_set_map(new_region_rid, default_map_rid)
  74. .. code-tab:: csharp 3D C#
  75. public partial class MyNode3D : Node3D
  76. {
  77. public override void _Ready()
  78. {
  79. Rid newRegionRid = NavigationServer3D.RegionCreate();
  80. Rid defaultMapRid = GetWorld3D().NavigationMap;
  81. NavigationServer3D.RegionSetMap(newRegionRid, defaultMapRid);
  82. }
  83. }
  84. .. note::
  85. Navigation regions can only be assigned to a single navigation map.
  86. If an existing region is assigned to a new navigation map it will leave the old map.