Godot Version
4.2.2.stable.official
Question
I have a CharacterBody2D based game and I’m trying to implement avoidance so entities don’t get stuck on each other. I followed the official tutorial here (Using NavigationAgents — Godot Engine (stable) documentation in English) in the section called “Actor as CharacterBody3D” and replaced the relevant 3D classes with 2D, but my entity moves extremely slowly regardless of the speed I set. I can set movment_speed to 100,000 and it makes no difference. Anyone know why this might be the case?
extends CharacterBody2D
@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D as NavigationAgent2D
@onready var sprite_2d_selected: Sprite2D = $Sprite2DSelected
@export var movement_speed: float = 1000.0
func _ready():
navigation_agent.velocity_computed.connect(_on_navigation_agent_velocity_computed)
func move_to_position(pos: Vector2):
navigation_agent.target_position = pos
if(!navigation_agent.is_target_reachable()):
navigation_agent.target_position = global_position
func _physics_process(delta):
if(navigation_agent.is_navigation_finished()):
return
var next_path_position: Vector2 = navigation_agent.get_next_path_position()
var new_velocity: Vector2 = global_position.direction_to(next_path_position) * movement_speed
if navigation_agent.avoidance_enabled:
navigation_agent.set_velocity(new_velocity)
else:
_on_navigation_agent_velocity_computed(new_velocity)
func _on_navigation_agent_velocity_computed(safe_velocity: Vector2):
velocity = safe_velocity
move_and_slide()