Godot Version
Godot 4.2.1 Stable
Question
`Trying to get a character to follow one of four possible random paths inside of a NavigationRegion2D. I know there is probably a simple solution however I cannot figure out why the navigation path is always starting at this top left navigation target (see screenshots) as opposed to from my character. So the character always just moves towards that starting point of the path.
I have it coded to queue_free the character when it reaches it’s target because my ultimate goal is to be able to, at the push of a button, instantiate many of this same character but conditionally have each instantiated character follow random paths and then be calculated into a score of some sort so the actual character will only be in the scene to follow the path and then be “gone”.
The path is clearly being calculated to the “random entrances” properly however, the starting point of the path is always this “entrance” (see screenshots) and not the character.
Here is my code for the character:
extends CharacterBody2D
@export var target : Node2D
@onready var navigationagent = $"../characternavigate/NavigationAgent2D"
@onready var entrance1 = $"../entrance-1"
@onready var entrance2 = $"../entrance-2"
@onready var entrance3 = $"../entrance-3"
@onready var entrance4 = $"../entrance-4"
@onready var entrances = [entrance1, entrance2, entrance3, entrance4]
var speed = 150
var acceleration = 2
var controlbool : bool
func _process(delta):
var direction = Vector3()
direction = navigationagent.get_next_path_position() - global_position
direction = direction.normalized()
velocity = velocity.lerp(direction * speed, acceleration * delta)
if controlbool:
move_and_slide()
func get_random_entrance():
entrances.shuffle()
var thisrandom = entrances[0]
return thisrandom
func _on_blue_button_down():
controlbool = true
target = get_random_entrance()
navigationagent.target_position = target.global_position
func _on_entrance_1_body_entered(body):
controlbool = false
body.queue_free()
func _on_entrance_2_body_entered(body):
controlbool = false
body.queue_free()
func _on_entrance_3_body_entered(body):
controlbool = false
body.queue_free()
func _on_entrance_4_body_entered(body):
controlbool = false
body.queue_free()
`
Please forgive me if this is just a total newbie question or the answer seems obvious, I diligently and honestly try to seek out as much information as possible before coming to the forums but I am having a tough time with these navigation properties and any help would be greatly appreciated.