gai-godot-games/pathfinding-algorithms/character.gd

24 lines
563 B
GDScript

extends CharacterBody2D
const MAX_SPEED: float = 300.0
const ACCELERATION: int = 2400
@export var nav: NavigationAgent2D
func _physics_process(delta: float) -> void:
var movement: Vector2 = Vector2()
nav.target_position = get_global_mouse_position()
movement = nav.get_next_path_position() - global_position
if movement.length() < 20:
movement = -velocity * 0.2
else:
movement = movement.normalized() * ACCELERATION * delta
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
move_and_slide()