dashing mechanic makes game crash

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

basically im trying to make a simple phone game that makes the character dash to the place where the player clicked on the screen.
the way i have this set up is i have a dash function:

func dash(pos):
	cur_state = states[2]
	dash_pos = pos
	dash_dir = (pos - position).normalized()

and here’s my physics process function + the calculate velocity function i have:

func _physics_process(delta):
  move_and_collide(CalculateVelocity())
  cur_state = states[0]
  if Input.is_action_just_pressed("ui_select"):
	jump()
  if Input.is_action_just_pressed("click"):
	dash(get_viewport().get_mouse_position())

func CalculateVelocity():
	if cur_state == states[1]: #if state is jumping
		velocity.y = 0 - jump_power * get_physics_process_delta_time()
	elif cur_state == states[2]: #if state is dashing
		while position != dash_pos:
			velocity = dash_dir
	else:
		velocity.y += gravity * get_physics_process_delta_time()
	return velocity

however this makes the game crash :(,
ANY HELP IS APPRECATED
thanks for taking a look

:bust_in_silhouette: Reply From: Andrea

with your current code, the game is stuck in the while loop
change while to if.
but even like this, i think your character will keep moving left and right relatively the dash pos, since it’s very unlikely that position+dash_dir==dash_posexactly.

maybe change the second if to

elif cur_state == states[2] and (position-dash_position).length()>0.1:

thanks for the answer i fixed everything up nicely but the dash won’t stop tho
still thanks a lot for your help :slight_smile:

ROBOTOO007 | 2021-01-11 13:30

i fixed everything thanks

ROBOTOO007 | 2021-01-11 13:35