Help with dashing towards the mouse

Godot Version



hello i am very very new to Godot so thank you for helping me. i am working on a 2d platformer and i am trying to add a dash to the game where the player moves towards the mouse and i got it working however when i dash diagonally it seems to go about half the distance as if i go straight up, down, left, or right, here is the script:

extends CharacterBody2D
@onready var sprite_0001_png = $"Sprite-0001_png"

const dash_time = 0.1
const dash_speed = 600
var dashing = false
var dash_charge = 1

var movement_lock = false
const SPEED = 250
const JUMP_VELOCITY = -600
var death = 0
var player_pos
var mouse_pos

func _process(delta):
	mouse_pos = get_global_mouse_position()
	player_pos = get_global_position()


var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func player_die():
	velocity = Vector2(0, 0)
	movement_lock = true
	await get_tree().create_timer(1).timeout
	dashing = true
	movement_lock = false

func _physics_process(delta):

	if not movement_lock == true and not is_on_floor():
		velocity.y += gravity * delta
	if is_on_floor() and dash_charge < 1:
		dash_charge += 1

	# Handle jump.
	if not movement_lock == true and Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	if not movement_lock == true:
		var direction = Input.get_axis("move_left", "move_right")
		if direction:
			velocity.x = direction * SPEED
			velocity.x = move_toward(velocity.x, 0, 15)
	var isright = velocity.x < 0
	sprite_0001_png.flip_h= isright
func dash():
	if dash_charge > 0 and not dashing:
		var dir
		if Input.is_action_just_pressed("dash"):
			velocity = Vector2(0, 0)
			movement_lock = true
			dash_charge -= 1
			dashing = true
			dir = (mouse_pos - player_pos)
			dir = dir.normalized()
			velocity = dir * dash_speed
			dash_charge -= 1
			dashing = true
			await get_tree().create_timer(dash_time).timeout
			dashing = false
			movement_lock = false

I guess it might seem that way to you because of the dir = dir.normalized(). Let’s say your player starts at (0, 0) and dashing to the right will put them at (1, 0), while dashing upwards will put them at (0, -1). However, if you’d now expect a diagonal dash with the same strength to end at (1, -1), you’d be wrong! The diagonal is longer.

Thank you for the help do you know to fix it I have no idea how lol

To fix what? This is the correct behavior if you want your character to move the same distance no matter if it’s horizontal/vertical or diagonal! If you don’t want this, just leave out the dir = dir.normalized() line in your script – but note that diagonal movement will be faster & reach further than horizontal or vertical movement then! It’s basically doing two moves in one, which rarely is what one would want.