try to get my dash mechanic to work as intended in topdown game

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

Hey I’m pretty new to coding, so i may be overlooking something simple, but i’ve looked through a lot of really similar questions and none have worked.
The character currently move in 8 directions with the arrow keys and the plan is to have the dash interrupt movement when clicked and dash a certain distance in the direction of the mouse.
the primary block of code for the dash is below, the first timer is the duration of the dash, and the second is the cooldown.

func dash():
if dash_pressed == 0:
	dashing = true
	
	$AnimatedSprite.play("dash up")
	velocity = Vector2(mouse_direction.normalized()) * dash_speed
	
	dash_pressed = 1
	$DashTimer.start()

func _on_DashTimer_timeout():
$AnimatedSprite.play("idle")
dashing = false

$DashTimer/DashCooldown.start()
func _on_DashCooldown_timeout():
dash_pressed = 0
:bust_in_silhouette: Reply From: SQBX

In your _process or _physics_process function (Wherever you are setting the velocity), use an if-statement prior to setting the velocity to check if the player is not currently dashing.

if not dashing:
   velocity = #Whatever you are setting velocity to

If you are changing velocity in multiple lines, put all of them within the if statement.

IMPORTANT: Don’t put your move_and_slide(velocity) method inside the if statement or the player will not move when they are dashing.

Hey i moved all my velocities over to the end statement and applied move and slide after the if statements but the dash is still wonky, it is dashing but it is an instantanious teleport, and i have no clue why, but the distance is huge, and seemingly completely disconnected from what my dash speed is. im honestly stumped but maybe it has ot do with how im getting mouse coordinates.

how im getting mouse co ordinates in the get input function which is called in the physics_process, the specific mouse co ordinate call is below.
Thanks for the help!

#dash wip
if Input.is_action_just_pressed("dash"):
	mouse_direction = get_local_mouse_position().normalized()
	dash()




#dash

func dash():
	if dash_pressed == 0:
		dashing = true
	
		$AnimatedSprite.play("dash up")
		dash_pressed = 1
		$DashTimer.start()
	
#dash timers
func _on_DashTimer_timeout():
	$AnimatedSprite.play("idle")
	dashing = false
	$DashTimer/DashCooldown.start()

func _on_DashCooldown_timeout():
	dash_pressed = 0


func _physics_process(delta):
	get_input()
	if dashing == false:
		velocity = move_speed * velocity
	else:
		velocity = dash_speed * Vector2(mouse_direction.normalized())
	velocity = move_and_slide(velocity)

consumetur | 2023-01-03 00:58

Change this line

velocity = dash_speed * Vector2(mouse_direction.normalized())

To

velocity = dash_speed * mouse_direction.normalized()

Because normalized() already returns a Vector2. If that still doesn’t work, dash_speed may be set to too high of a value. Try setting it to a really low value and increase it as needed.

Tell me if you need any more help

SQBX | 2023-01-03 01:44

I took out the secondary vector two and went all the way down to 1 for dash speed and it still goes a ridiculous distance.
not really sure what the issue could be, is there a way i could send a copy of the project?
or would it help if i just sent the entire code for the player.

consumetur | 2023-01-04 05:43