So this is my code. I wanna create a sprint, for example: when I press "shift", the speed of walk double or something like a speed bonus. Like a "walk to run" mechanic.
extends KinematicBody2D
const TARGET_FPS = 60
const ACCELERATION = 8
const MAX_SPEED = 64
const FRICTION = 10
const AIR_RESISTANCE = 1
const GRAVITY = 4
const JUMP_FORCE = 140
var motion = Vector2.ZERO
onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer
func _physics_process(delta):
var x_input = Input.get_action_strength("right") - Input.get_action_strength("left")
if x_input != 0:
animationPlayer.play("Walk")
motion.x += x_input * ACCELERATION * delta * TARGET_FPS
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
sprite.flip_h = x_input < 0
else:
animationPlayer.play("Idle")
motion.y += GRAVITY * delta * TARGET_FPS
if is_on_floor():
if x_input == 0:
motion.x = lerp(motion.x, 0, FRICTION * delta)
if Input.is_action_just_pressed("jump"):
motion.y = -JUMP_FORCE
else:
animationPlayer.play("Jump")
if Input.is_action_just_released("jump") and motion.y < -JUMP_FORCE/2:
motion.y = -JUMP_FORCE/2
if x_input == 0:
motion.x = lerp(motion.x, 0, AIR_RESISTANCE * delta)
motion = move_and_slide(motion, Vector2.UP)