Godot Version
4.5.1
Question
Hey, I’ve been having problems with the navigation region 2d. I’m decently know to Godot, so I’ve been following a tutorial series on YouTube (here’s the one thats not working: https://www.youtube.com/watch?v=1EkJUdfnVzk&list=PLWTXKdBN8RZe3ytf6qdR4g1JRy0j-93v9&index=11) the problem I’m having is the character keeps moving to the last point of the navigation region 2d (the last point the makes the region a 2d shape). I’ve rewatched the video multiple times but i cant get it to work.
I’ve attached the script with the path finding.
PS. I’m was trying to get this done by Christmas as its a gift for a friend, so any and all help is appreciated. thanks!
extends NodeState
@export var Character: CharacterBody2D
@export var AnimatedSprite: AnimatedSprite2D
@export var Navigation_Agent_2d: NavigationAgent2D
@export var minSpeed: float = 5.0
@export var maxSpeed: float = 10.0
var speed: float
func _ready() -> void:
call_deferred("character_setup")
func character_setup() -> void:
await get_tree().physics_frame
set_movement_target()
func set_movement_target() -> void:
var targetPosition: Vector2 = NavigationServer2D.map_get_random_point(Navigation_Agent_2d.get_navigation_map(), Navigation_Agent_2d.navigation_layers, false)
Navigation_Agent_2d.target_position = targetPosition
speed = randf_range(minSpeed, maxSpeed)
func _on_process(_delta : float) -> void:
pass
func _on_physics_process(_delta : float) -> void:
#var target_Position: Vector2 = Navigation_Agent_2d.get_next_path_position()
var targetDirection: Vector2 = Character.global_position.direction_to(Navigation_Agent_2d.get_next_path_position())
AnimatedSprite.flip_h = targetDirection.x < 0
Character.velocity = targetDirection * speed
Character.move_and_slide()
func _on_next_transitions() -> void:
pass
func _on_enter() -> void:
AnimatedSprite.play("Walk")
func _on_exit() -> void:
AnimatedSprite.Stop()
