i have a 2D character movement script, and want to make it so that when a key is pressed, the character starts bouncing like a ball. unfortunately, i have no idea how to even begin working on it.

character script btw:

extends CharacterBody2D

@export var SPEED = 200.0
@export var RUN_SPEED = 500.0
@export var JUMP_VELOCITY = -275.0
@export var ACCELERATION = 1000000000.0
@export var FRICTION = 1000000000.0

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var running = false
var drifting = false
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var drift_var_timer = $DriftVarTimer

func _physics_process(delta):
	apply_gravity(delta)
	handle_jump()
	var input_axis = Input.get_axis("left", "right")
	handle_movement(input_axis, delta)
	drift(input_axis, delta)
	handle_run()
	end_run()
	update_animations(input_axis)
	move_and_slide()

func apply_gravity(delta):
	if not is_on_floor():
		velocity.y += gravity * delta

func handle_jump():
	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			velocity.y = JUMP_VELOCITY
	else:
			if Input.is_action_just_released("jump") and velocity.y < JUMP_VELOCITY / 2:
				velocity.y = JUMP_VELOCITY / 2

func handle_movement(input_axis, delta):
	if input_axis != 0:
		velocity.x = move_toward(velocity.x, SPEED * input_axis, ACCELERATION * delta)

func drift(input_axis, delta):
	if input_axis == 0:
		velocity.x = move_toward(velocity.x, 0, FRICTION * delta)

func handle_run():
	if Input.is_action_just_pressed("run"):
		SPEED = RUN_SPEED
		ACCELERATION = 500.0
		FRICTION = 900.0
		running = true

func end_run():
	if Input.is_action_just_released("run"):
		SPEED = 200.0
		ACCELERATION = 1000000000.0
		FRICTION = 1000000000.0
		running = false

func update_animations(input_axis):
	if is_on_floor():
		if Input.is_action_pressed("left") or Input.is_action_pressed("right"):
			if $AnimatedSprite2D.is_playing():
				if running && Input.is_action_pressed("run") && input_axis != 0:
					$AnimatedSprite2D.play("run")
				else:
					$AnimatedSprite2D.play("walk")
		else:
			$AnimatedSprite2D.play("idle")

	if is_on_floor():
		if Input.is_action_pressed("left") and Input.is_action_pressed("right"):
			$AnimatedSprite2D.play("idle")

	if is_on_floor() and Input.is_action_pressed("jump"):
		$AnimatedSprite2D.play("jump_start")

	if not is_on_floor():
		if not Input.is_action_pressed("jump"):
			$AnimatedSprite2D.play("fall")

	if(velocity.x < 0):
		scale.x = scale.y * -1
	elif(velocity.x > 0):
		scale.x = scale.y * 1```

There is a function called .bounce() that should do a lot of the work for you.

The docs are your friend!

So just set a velocity in whatever direction when you press that button, apply gravity in the physics process, use .bounce() with your physics calculation as the argument and you're done.

Oh, and double check when you paste code. Sometimes Markdown doesn't import the clipboard correctly if you hit a wrong button somewhere. Just put it all between tildes (the ~ sign but 3x on each end)

LIKE THIS:

### three tildes at start ###
var variable : int = 0
var facts : bool = true
### three tildes at end###

    SnapCracklins

    thank you for the tips, but wouldn't the bounce() function just make the ball act like one in Breakout as opposed to an actual bouncing ball (like a basketball)?

      thatoneguy don't trigger the bounce until your character is landing. have the value of their bounce cut in half (or by a third or whatever) each time they bounce, your gravity will keep pulling you down. You just need to change that velocity a bit every bounce. (and maybe the speed too.)

      lewyche

      thank you for the suggestion, but (out of curiosity), is there a way to make the movement work with a CharacterBody2D?