支持不同角色类型

../../_images/nav_actor_sizes.png

要支持不同的角色类型,例如尺寸不同的角色,每种类型的角色都需要有各自的导航地图、使用合适的代理半径以及高度烘焙导航网格。也可以使用相同的做法来区分地面行走、水中游泳、空中飞翔的代理。

备注

Agents are exclusively defined by a radius and height value for baking navigation meshes, pathfinding and avoidance. More complex shapes are not supported.

GDScript

  1. # Create a navigation mesh resource for each actor size.
  2. var navigation_mesh_standard_size: NavigationMesh = NavigationMesh.new()
  3. var navigation_mesh_small_size: NavigationMesh = NavigationMesh.new()
  4. var navigation_mesh_huge_size: NavigationMesh = NavigationMesh.new()
  5. # Set appropriated agent parameters.
  6. navigation_mesh_standard_size.agent_radius = 0.5
  7. navigation_mesh_standard_size.agent_height = 1.8
  8. navigation_mesh_small_size.agent_radius = 0.25
  9. navigation_mesh_small_size.agent_height = 0.7
  10. navigation_mesh_huge_size.agent_radius = 1.5
  11. navigation_mesh_huge_size.agent_height = 2.5
  12. # Get the root node to parse geometry for the baking.
  13. var root_node: Node3D = get_node("NavigationMeshBakingRootNode")
  14. # Create the source geometry resource that will hold the parsed geometry data.
  15. var source_geometry_data: NavigationMeshSourceGeometryData3D = NavigationMeshSourceGeometryData3D.new()
  16. # Parse the source geometry from the scene tree on the main thread.
  17. # The navigation mesh is only required for the parse settings so any of the three will do.
  18. NavigationServer3D.parse_source_geometry_data(navigation_mesh_standard_size, source_geometry_data, root_node)
  19. # Bake the navigation geometry for each agent size from the same source geometry.
  20. # If required for performance this baking step could also be done on background threads.
  21. NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_standard_size, source_geometry_data)
  22. NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_small_size, source_geometry_data)
  23. NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_huge_size, source_geometry_data)
  24. # Create different navigation maps on the NavigationServer.
  25. var navigation_map_standard: RID = NavigationServer3D.map_create()
  26. var navigation_map_small: RID = NavigationServer3D.map_create()
  27. var navigation_map_huge: RID = NavigationServer3D.map_create()
  28. # Set the new navigation maps as active.
  29. NavigationServer3D.map_set_active(navigation_map_standard, true)
  30. NavigationServer3D.map_set_active(navigation_map_small, true)
  31. NavigationServer3D.map_set_active(navigation_map_huge, true)
  32. # Create a region for each map.
  33. var navigation_region_standard: RID = NavigationServer3D.region_create()
  34. var navigation_region_small: RID = NavigationServer3D.region_create()
  35. var navigation_region_huge: RID = NavigationServer3D.region_create()
  36. # Add the regions to the maps.
  37. NavigationServer3D.region_set_map(navigation_region_standard, navigation_map_standard)
  38. NavigationServer3D.region_set_map(navigation_region_small, navigation_map_small)
  39. NavigationServer3D.region_set_map(navigation_region_huge, navigation_map_huge)
  40. # Set navigation mesh for each region.
  41. NavigationServer3D.region_set_navigation_mesh(navigation_region_standard, navigation_mesh_standard_size)
  42. NavigationServer3D.region_set_navigation_mesh(navigation_region_small, navigation_mesh_small_size)
  43. NavigationServer3D.region_set_navigation_mesh(navigation_region_huge, navigation_mesh_huge_size)
  44. # Create start and end position for the navigation path query.
  45. var start_pos: Vector3 = Vector3(0.0, 0.0, 0.0)
  46. var end_pos: Vector3 = Vector3(2.0, 0.0, 0.0)
  47. var use_corridorfunnel: bool = true
  48. # Query paths for each agent size.
  49. var path_standard_agent = NavigationServer3D.map_get_path(navigation_map_standard, start_pos, end_pos, use_corridorfunnel)
  50. var path_small_agent = NavigationServer3D.map_get_path(navigation_map_small, start_pos, end_pos, use_corridorfunnel)
  51. var path_huge_agent = NavigationServer3D.map_get_path(navigation_map_huge, start_pos, end_pos, use_corridorfunnel)

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.