I made two NavigationRegions, one for beings inside water, and one for ground, the default navigationregion that the entity chooses, is the water, and i’m trying to make it choose between those 2 options based of the navigation_type variable.
I got the rid, but for some reason set_navigation_map() does not change anything? please help me. (There are no errors in the debug menu)
@export var navigation_type = "ground" #set as either "ground" or "water"
func _ready():
update_target_position()
await get_tree().create_timer(1).timeout
if navigation_type == "ground":
var Navigation_ground = get_parent().find_child("Navigation ground")
var navigation_mesh = Navigation_ground.navigation_mesh
print(navigation_mesh)
agent.set_navigation_map(navigation_mesh)
print(agent.get_navigation_map())
finished_start_up = true
Output of code:
<NavigationMesh#-9223372004709628554>
RID(5982889443328)
I have a gridmap parent which uses this script to replace tiles with scenes with this script:
extends GridMap
var cells = get_used_cells()
var water = preload("res://water.tscn")
var floor = preload("res://floor.tscn")
var plank = preload("res://water_with_plank.tscn")
@onready var navigation_ground = $"Navigation ground"
@onready var navigation_water = $"Navigation water"
func _ready():
replace_cells_with_scenes()
call_deferred("bake_navi")
func replace_cells_with_scenes():
for cell in cells:
var rand = randi_range(1, 10)
if rand == 1 and get_cell_item(cell) == 0:
var real_position = map_to_local(cell)
var wall_inst = water.instantiate()
set_cell_item(cell, -1)
wall_inst.position = real_position
navigation_water.add_child(wall_inst)
var water_cells = get_used_cells_by_item(1)
for cell in water_cells:
var real_position = map_to_local(cell)
var inst = water.instantiate()
set_cell_item(cell, -1)
inst.position = real_position
navigation_water.add_child(inst)
var floor_cells = get_used_cells_by_item(0)
for cell in floor_cells:
var real_position = map_to_local(cell)
var inst = floor.instantiate()
set_cell_item(cell, -1)
inst.position = real_position
navigation_ground.add_child(inst)
var plank_cells = get_used_cells_by_item(2)
for cell in plank_cells:
var real_position = map_to_local(cell)
var inst = plank.instantiate()
set_cell_item(cell, -1)
inst.position = real_position
navigation_ground.add_child(inst)
func bake_navi():
navigation_water.bake_navigation_mesh()
await get_tree().create_timer(1).timeout
navigation_ground.bake_navigation_mesh()
The scenes are then added as a child to the two navigation regions, which are children of the gridmap.
Then the gridmap navigation meshes are baked with bake_navi.
full script of the entity:
extends CharacterBody3D
@onready var agent = $NavigationAgent3D
@export var navigation_type = "ground" #set as: "water" "ground"
const SPEED = 40.0
var target_position:Vector3
var finished_start_up:bool = false
@export var player:CharacterBody3D
func _ready():
update_target_position()
await get_tree().create_timer(1).timeout
if navigation_type == "ground":
var Navigation_ground = get_parent().find_child("Navigation ground")
var navigation_mesh = Navigation_ground.navigation_mesh
print(navigation_mesh)
agent.set_navigation_map(navigation_mesh)
print(agent.get_navigation_map())
finished_start_up = true
func _physics_process(delta):
if finished_start_up:
var current_location = global_transform.origin
var next_location = agent.get_next_path_position()
var new_velocity = (next_location - current_location).normalized() * SPEED
velocity = new_velocity
move_and_slide()
func update_target_position():
agent.target_position = player.global_position