2
0
Эх сурвалжийг харах

Add C# code to "Using NavigationServer"

Raul Santos 1 жил өмнө
parent
commit
3a23253734

+ 63 - 10
tutorials/navigation/navigation_using_navigationservers.rst

@@ -95,38 +95,38 @@ Afterwards the function waits for the next physics frame before continuing with
     extends Node3D
 
     func _ready():
-        # use call deferred to make sure the entire scene tree nodes are setup
-        # else await / yield on 'physics_frame' in a _ready() might get stuck
+        # Use call deferred to make sure the entire scene tree nodes are setup
+        # else await on 'physics_frame' in a _ready() might get stuck.
         call_deferred("custom_setup")
 
     func custom_setup():
 
-        # create a new navigation map
+        # Create a new navigation map.
         var map: RID = NavigationServer3D.map_create()
         NavigationServer3D.map_set_up(map, Vector3.UP)
         NavigationServer3D.map_set_active(map, true)
 
-        # create a new navigation region and add it to the map
+        # Create a new navigation region and add it to the map.
         var region: RID = NavigationServer3D.region_create()
         NavigationServer3D.region_set_transform(region, Transform3D())
         NavigationServer3D.region_set_map(region, map)
 
-        # create a procedural navigation mesh for the region
+        # Create a procedural navigation mesh for the region.
         var new_navigation_mesh: NavigationMesh = NavigationMesh.new()
         var vertices: PackedVector3Array = PackedVector3Array([
-            Vector3(0,0,0),
-            Vector3(9.0,0,0),
-            Vector3(0,0,9.0)
+            Vector3(0, 0, 0),
+            Vector3(9.0, 0, 0),
+            Vector3(0, 0, 9.0)
         ])
         new_navigation_mesh.set_vertices(vertices)
         var polygon: PackedInt32Array = PackedInt32Array([0, 1, 2])
         new_navigation_mesh.add_polygon(polygon)
         NavigationServer3D.region_set_navigation_mesh(region, new_navigation_mesh)
 
-        # wait for NavigationServer sync to adapt to made changes
+        # Wait for NavigationServer sync to adapt to made changes.
         await get_tree().physics_frame
 
-        # query the path from the navigationserver
+        # Query the path from the navigation server.
         var start_position: Vector3 = Vector3(0.1, 0.0, 0.1)
         var target_position: Vector3 = Vector3(1.0, 0.0, 1.0)
         var optimize_path: bool = true
@@ -141,6 +141,59 @@ Afterwards the function waits for the next physics frame before continuing with
         print("Found a path!")
         print(path)
 
+ .. code-tab:: csharp C#
+
+    using Godot;
+
+    public partial class MyNode3D : Node3D
+    {
+        public override void _Ready()
+        {
+            // Use call deferred to make sure the entire scene tree nodes are setup
+            // else await on 'physics_frame' in a _Ready() might get stuck.
+            CallDeferred(MethodName.CustomSetup);
+        }
+
+        private async void CustomSetup()
+        {
+            // Create a new navigation map.
+            Rid map = NavigationServer3D.MapCreate();
+            NavigationServer3D.MapSetUp(map, Vector3.Up);
+            NavigationServer3D.MapSetActive(map, true);
+
+            // Create a new navigation region and add it to the map.
+            Rid region = NavigationServer3D.RegionCreate();
+            NavigationServer3D.RegionSetTransform(region, Transform3D.Identity);
+            NavigationServer3D.RegionSetMap(region, map);
+
+            // Create a procedural navigation mesh for the region.
+            var newNavigationMesh = new NavigationMesh()
+            {
+                Vertices = new[]
+                {
+                    new Vector3(0.0f, 0.0f, 0.0f),
+                    new Vector3(9.0f, 0.0f, 0.0f),
+                    new Vector3(0.0f, 0.0f, 9.0f),
+                },
+            };
+            int[] polygon = new[] { 0, 1, 2 };
+            newNavigationMesh.AddPolygon(polygon);
+            NavigationServer3D.RegionSetNavigationMesh(region, newNavigationMesh);
+
+            // Wait for NavigationServer sync to adapt to made changes.
+            await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
+
+            // Query the path from the navigation server.
+            var startPosition = new Vector3(0.1f, 0.0f, 0.1f);
+            var targetPosition = new Vector3(1.0f, 0.0f, 1.0f);
+
+            Vector3[] path = NavigationServer3D.MapGetPath(map, startPosition, targetPosition, optimize: true);
+
+            GD.Print("Found a path!");
+            GD.Print((Variant)path);
+        }
+    }
+
 Server Avoidance Callbacks
 ~~~~~~~~~~~~~~~~~~~~~~~~~~