- Edited
So I followed HeartBeast's tutorial on YouTube for making a 2D platformer. I've been able to implement a double jump feature to the player. But I'm struggling to work on the dash feature. I can do it using a separate dash button but what I'm trying to do is make the player dash only when double-tapping the arrow keys.
EDIT - Also, for some reason, my acceleration is not working. I'm new to Godot so sorry if I've made some stupid mistakes in the code.
I've pasted the code for reference -
extends KinematicBody2D
const UP = Vector2(0, -1)
const GRAVITY = 20
var MAX_SPEED = 250
const JUMP_HEIGHT = -500
const ACC = 50
var motion = Vector2()
var friction = false
var djump = -1
var rtap = 0
var dash = -1
func _ready():
pass
func _physics_process(delta):
motion.y += GRAVITY
dash = -1
if Input.is_action_pressed("ui_right"):
motion.x = min(motion.x + ACC, MAX_SPEED)
$Sprite.flip_h = false
$Sprite.play("Run")
if dash!=0:
#double_tap_dash()
if Input.is_action_just_released("ui_right"):
rtap = 1
$DashStart.start()
if Input.is_action_just_pressed("ui_right") && rtap!=2:
#motion.x=2000
MAX_SPEED == 2000
print(motion.x)
#$Sprite.flip_h = false
#$Sprite.play("Fall") #"Dash"
rtap = 2
$DashMid.start()
$DashEnd.start()
elif Input.is_action_pressed("ui_left"):
motion.x = max(motion.x - ACC, -MAX_SPEED)
$Sprite.flip_h = true
$Sprite.play("Run")
if dash!=0:
#double_tap_dash()
if Input.is_action_just_released("ui_left"):
rtap = 1
$DashStart.start()
if Input.is_action_just_pressed("ui_left") && rtap!=2:
#motion.x=-2000
MAX_SPEED == -2000
print(motion.x)
#$Sprite.flip_h = true
#$Sprite.play("Fall") #"Dash"
rtap = 2
$DashMid.start()
$DashEnd.start()
else:
friction = true;
$Sprite.play("Idle")
if is_on_floor():
djump = -1
if Input.is_action_just_pressed("ui_up"):
motion.y = JUMP_HEIGHT
djump = 1
if friction == true:
motion.x = lerp(motion.x, 0, 0.15)
else:
if Input.is_action_just_pressed("ui_down"):
motion.y = max(motion.y + ACC, MAX_SPEED)
#if Input.is_action_just_pressed("ui_up"):
# motion.y = JUMP_HEIGHT
$Sprite.play("Jump")
if(motion.y < 0):
$Sprite.play("Jump")
else:
$Sprite.play("Fall")
if djump != 0:
if Input.is_action_just_pressed("ui_up") && djump == 1:
motion.y = JUMP_HEIGHT # + 150
djump = 0
if friction == true:
motion.x = lerp(motion.x, 0, 0.15)
if Input.is_action_just_pressed("ui_up") && djump != 0:
motion.y = JUMP_HEIGHT # + 150
djump = 0
motion = move_and_slide(motion, UP)
pass
func double_tap_dash():
if Input.is_action_just_released("ui_right"):
rtap = 1
$DashStart.start()
if Input.is_action_just_pressed("ui_right") && rtap!=2:
#motion.x=2000
MAX_SPEED == 2000
#$Sprite.flip_h = false
#$Sprite.play("Fall") #"Dash"
rtap = 2
if Input.is_action_just_released("ui_left"):
rtap = 1
$DashStart.start()
if Input.is_action_just_pressed("ui_left") && rtap!=2:
#motion.x=-2000
MAX_SPEED == 2000
#$Sprite.flip_h = true
#$Sprite.play("Fall") #"Dash"
rtap = 2
#return motion.x
func _on_DashStart_timeout():
pass # Replace with function body.
func _on_DashEnd_timeout():
dash=1
rtap=2
pass # Replace with function body.
func _on_DashMid_timeout():
dash=0
rtap=0
MAX_SPEED == 250
pass # Replace with function body.