Improving the character movement code in 2D space

Godot Version

Godot Engine v4.4.1

Question

I’m currently writing code to control a character in 2D space, can you tell me what can be added or improved?

Code:

> extends CharacterBody2D
> 
> @export var max_speed: float = 300.0
> @export var acceleration: float = 800.0
> @export var deceleration: float = 1200.0
> @export var inertia_factor: float = 0.85
> @export var rotation_smoothness: float = 0.15
> @export var animation_transition_speed: float = 0.1
> 
> @export var min_animation_speed: float = 0.5
> @export var max_animation_speed: float = 1.5
> 
> @onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
> 
> var current_velocity: Vector2 = Vector2.ZERO
> var target_direction: Vector2 = Vector2.ZERO
> var is_moving: bool = false
> var current_flip_h: float = 0.0 
> var current_animation_speed: float = 1.0
> 
> func _physics_process(delta: float) -> void:
>     var raw_input = Input.get_vector("left", "right", "up", "down")
>     target_direction = target_direction.lerp(raw_input, 0.2)
>     var target_velocity = target_direction * max_speed
>     
>     var current_accel = acceleration if target_direction != Vector2.ZERO else deceleration
>     current_velocity = current_velocity.move_toward(target_velocity, current_accel * delta)
>     
>     var inertia = inertia_factor * (1.0 - target_direction.length())
>     current_velocity = lerp(current_velocity, target_velocity, 1.0 - inertia)
>     
>     is_moving = current_velocity.length() > 10.0
>     
>     velocity = current_velocity
>     move_and_slide()
>     
>     update_sprite_rotation(delta)
>     update_animations()
> 
> func update_sprite_rotation(delta: float):
>     if abs(velocity.x) > 5:
>         var target_flip = 1.0 if velocity.x > 0 else 0.0
>         current_flip_h = lerp(current_flip_h, target_flip, rotation_smoothness * delta * 60)
>         animated_sprite.flip_h = current_flip_h > 0.5  # Преобразуем обратно в bool
> 
> func update_animations():
>     var speed_ratio = clamp(current_velocity.length() / max_speed, 0.0, 1.0)
>     var target_speed = lerp(min_animation_speed, max_animation_speed, speed_ratio)
>     current_animation_speed = lerp(current_animation_speed, target_speed, animation_transition_speed)
>     animated_sprite.speed_scale = current_animation_speed
>     
>     if is_moving:
>         if animated_sprite.animation != "run":
>             animated_sprite.play("run")
>     else:
>         if animated_sprite.animation != "idle":
>             animated_sprite.play("idle")
> 
> func get_movement_intensity() -> float:
>     return clamp(current_velocity.length() / max_speed, 0.0, 1.0)
> 
> func get_current_direction() -> Vector2:
>     return current_velocity.normalized() if current_velocity.length() > 5 else Vector2.ZERO

Avoid using lerp for important game functions, this line of code is now frame-dependent because of lerp. Lerp is always frame-dependent, even if you multiply t by delta. Such as here:

Using move_toward achieves a similar effect with sensible units; like what unit is rotation_smoothness? It is not radians or degress per-second, it’s a per-frame value multiplied by per-frame-time factor of the difference between two values, which I doubt is what you want.