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


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"):

# 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"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"fall")
		if (velocity.x == 0):"idle")

	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

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

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)
		velocity.x = clampf(velocity.x + decay, thrust_cap*-1, 0)

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


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: