Making a momentum based MovementComponent

Godot Version 4.7

Hello there, once again I’m back with more easy to answer(I hope) questions. Today I bring you the mystery of… why does my code not work? As the title says, I’m doing a MovementComponent but now I want to add momentum to it. This comes from wanting to add a DashComponent, but that is another can of worms, the point is I did this:

class_name PlayerMovement extends Node
@export_subgroup(“Character”)
@export var Player:           CharacterBody2D
@export var Body:             Sprite2D

@export_subgroup(“Horizontal Movement”)
@export var max_speed:        float
@export var acceleration:     float
@export var friction:         float

var direction: Vector2 = Vector2.ZERO

func tick(delta : float) → void:

 if Player == null:
  return

 if direction != Vector2.ZERO:
  var target_velocity = Vector2(direction * max_speed)
  Player.velocity.x = move_toward(Player.velocity.x,target_velocity, acceleration * delta)
  Player.velocity.y = move_toward(Player.velocity.y,target_velocity, acceleration * delta)

 else:
  Player.velocity.x = move_toward(Player.velocity.x, 0 , friction * delta)
  Player.velocity.x = move_toward(Player.velocity.y, 0, friction * delta)

 Player.move_and_slide() 

And of course, I have this error message:

E 0:00:14:065 PlayerMovement.tick: Invalid type in utility function “move_toward()”. Cannot convert argument 2 from Vector2 to float.
Player_Movement.gd:20 @ PlayerMovement.tick()
Player_Movement.gd:20 @ tick()
Quacky_Main_Script.gd:12 @ _physics_process()

This Happens every time I try to move in the code, here is the player code:

class_name QuackyMainScript extends CharacterBody2D

@onready var player_input = $PlayerInput
@onready var player_movement = $PlayerMovement
@onready var dash = $Dash

@export  var Body: Sprite2D
@export  var Arm:  Sprite2D

func _physics_process(delta:float) → void:
 player_input._update()
 player_movement.tick(delta)
 player_movement.direction = player_input.dir_movement

 var mouse_position = get_global_mouse_position()
 Arm.look_at(mouse_position)
 Arm.rotation += deg_to_rad(270)

 if Body.global_position.x < mouse_position.x:
	Body.flip_h = true
 else:
	Body.flip_h = false

Anyway, If you know how to correct this, pls help!

Looks like you forgot to use target_velocity.x and target_velocity.y in your move towards lines.

That indeed fixed the issue of the error, but now that it runs, when I apply one vertical and horizontal speed (pushed a diagonal direction) the moment I stop the character goes in one of two directions, either (-x,-y) or (x,y) at a 90°angle, generally with more speed than specified

nvm, the code was wrong in the line Player.velocity.x = move_toward(Player.velocity.y, 0, friction * delta) it must be Player.velocity.y = move_toward(Player.velocity.y, 0, friction * delta)

that fixes the issue, now its only a matter of adjusting the values in the editor to one’s preferences for the character, thanks!!