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

22 lines
476 B
GDScript
Raw Normal View History

2024-11-05 10:44:20 +01:00
extends CharacterBody2D
const MAX_SPEED = 300.0
const ACCELERATION = 30
@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
movement = movement.normalized() * ACCELERATION
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
move_and_slide()