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```