Do not know how to clamp max speed in 2d?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GodotBeginner2023

I’m making a top-down 2d game and following a tutorial, but it’s made in version 3 and I’m not sure how to convert the code without disrupting it. I know that clamp requires two arguments, but idk what to use.

extends CharacterBody2D
class_name Character

const FRICTION: float = 0.15
@export var acceleration: int = 40
@export var max_speed: int = 100

var mov_direction: Vector2 = Vector2.ZERO

func _physics_process(_delta: float) -> void:
	move_and_slide()
	velocity = lerp(velocity, Vector2.ZERO, FRICTION)
	
func move() -> void:
	mov_direction = mov_direction.normalized()
	velocity += mov_direction * acceleration
	velocity = velocity.clamp(max_speed)
:bust_in_silhouette: Reply From: jgodfrey

To clamp a Vector2 in Godot 4, you want limit_length(). So…

velocity = velocity.limit_length(max_speed)