Player with floating/hover movement: How to do?

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

Hi,

i am trying to make a 2d sideview game where the player delves into a dungeon. Running left and right, no jumping. Player can fall and switch to hover mode (Maybe like the old HERO game on atari 2600). Switching to hover retains falling speed while switching midair. Fall damage while hitting floor too hard. Gliding on walls while hovering.

I dont get this to work. I seem to be too stupid.

Does anybody know i tutorial or for this kind of player movement? I find only stuff for traditional jump and run. Floating/hovering not.

I got this.

func _physics_process(delta):
if Input.is_action_pressed("Exit NOW"):
	get_tree().quit()

# handle switching float and walk
if Input.is_action_just_pressed("Toggle Float"):

	if _state == States.GROUND:
		motion_mode = CharacterBody2D.MOTION_MODE_FLOATING
		_state = States.FLOAT
		animated_sprite_2d.play("hover")
	elif _state == States.FLOAT:
		motion_mode = CharacterBody2D.MOTION_MODE_GROUNDED
		_state = States.GROUND

if _state == States.GROUND:
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		animated_sprite_2d.play("fall")
	else:
		if (velocity.x == 0):
			animated_sprite_2d.play("idle")
		else:
			animated_sprite_2d.play("run")

	var direction_h = Input.get_axis("ui_left", "ui_right")
	if direction_h:
		velocity.x = direction_h * SPEED

		if (velocity.x < 0):
			animated_sprite_2d.flip_h = true
		elif (velocity.x > 0):
			animated_sprite_2d.flip_h = false

	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
elif _state == States.FLOAT:

	var left = Input.is_action_pressed("ui_left")
	var right = Input.is_action_pressed("ui_right")
	var up = Input.is_action_pressed("ui_up")
	var down = Input.is_action_pressed("ui_down")

	if left:
		velocity.x -= thrust_amount
	if right:
		velocity.x += thrust_amount

	velocity.x = clampf(velocity.x, thrust_cap*-1, thrust_cap)

	if up:
		velocity.y -= thrust_amount
	if down:
		velocity.y += thrust_amount

	velocity.y = clampf(velocity.y, thrust_cap*-1, thrust_cap)

	if (velocity.x < 0):
		animated_sprite_2d.flip_h = true
	elif (velocity.x > 0):
		animated_sprite_2d.flip_h = false

	move_toward(velocity.x, velocity.y, SPEED)

	# decay thrust
	var decay = 0.9
	if velocity.x > 0:
		velocity.x = clampf(velocity.x - decay, 0, thrust_cap)
	else:
		velocity.x = clampf(velocity.x + decay, thrust_cap*-1, 0)

	if velocity.y > 0:
		velocity.y = clampf(velocity.y - decay, 0, thrust_cap)
	else:
		velocity.y = clampf(velocity.y + decay, thrust_cap*-1, 0)

	move_and_slide()

It works…kind of. Switching to hover and back works. I want to max the thrust when you push the buttons. All in all it behaves weird.

Thanks for the help or tips guys.

:bust_in_silhouette: Reply From: aidave

CharacterBody2D has a Motion Mode to help with this: