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

21 lines
515 B
GDScript
Raw Normal View History

2024-11-05 10:44:20 +01:00
extends CharacterBody2D
2024-11-05 12:16:17 +01:00
const MAX_SPEED: float = 300.0
const ACCELERATION: int = 30
2024-11-05 10:44:20 +01:00
@export var nav: NavigationAgent2D
2024-11-05 12:16:17 +01:00
2024-11-05 10:44:20 +01:00
func _physics_process(delta: float) -> void:
2024-11-05 12:16:17 +01:00
var movement: Vector2 = Vector2()
2024-11-05 10:44:20 +01:00
2024-11-05 12:16:17 +01:00
nav.target_position = get_global_mouse_position()
2024-11-05 10:44:20 +01:00
2024-11-05 12:16:17 +01:00
movement = nav.get_next_path_position() - global_position
movement = movement.normalized() * ACCELERATION
2024-11-05 10:44:20 +01:00
2024-11-05 12:16:17 +01:00
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
2024-11-05 10:44:20 +01:00
2024-11-05 12:16:17 +01:00
move_and_slide()