I'm developing my first game in godot, I've been trying to create a platformer with simple mechanics such as dash and jump.
I created a timer called dash_timer to create a gap between dashes, after the execution of the dash the dash timer waits for 0.8 secs and then resets the can_dash value to TRUE so the player can dash again.
But this implementation doesn't work when the player's in mid air, the player can basically dash infinitely in mid air.
I can't seem to come up with a possible solution as i'm new to programming and godot as well, any help would be appreciated!!
Here's the player script:
extends KinematicBody2D
const MAX_FALL_SPEED = 650
const MAX_JUMP_TIME = 150
onready var anim_player = $AnimationPlayer
onready var sprite = $Sprite
var move_speed = 250****
var jump_force = 1500
var gravity = 120
var can_dash = true
var y_velo = 0
var facing_right = true
var hang_time: float = 0.2
var hang_counter: float = 0
var jump_buffer_length: float = 0.5
var jump_buffer_count: float = 0
var is_dashing = false
func _physics_process(delta):
var is_jump_interrupted = Input.is_action_just_released("jump") and y_velo < 0
var move_dir = 0
#INPUT HANDLING(LEFT AND RIGHT)
if Input.is_action_pressed("move_right"):
move_dir += 1
if Input.is_action_pressed("move_left"):
move_dir -= 1
if is_jump_interrupted:
y_velo = 0
if Input.is_action_just_pressed("dash"):
if can_dash == true:
dash()
move_and_slide(Vector2(move_dir * move_speed, y_velo), Vector2(0, -1))
var grounded = is_on_floor()
if !is_dashing:
y_velo += gravity
else:
y_velo = 0
#COYOTE JUMPING
if is_on_floor():
hang_counter = hang_time
else:
hang_counter -= delta
#MANAGING JUMP BUFFER
if Input.is_action_just_pressed("jump"):
jump_buffer_count = jump_buffer_length
else:
jump_buffer_count -= delta
#JUMPING
if hang_counter > 0 and jump_buffer_count >= 0:
y_velo = -jump_force
jump_buffer_count = 0
if grounded and y_velo >= 5:
y_velo = 5
if y_velo > MAX_FALL_SPEED:
y_velo = MAX_FALL_SPEED
#FLIPPING THE SPRITE IN THE RIGHT DIRECTION
if facing_right and move_dir < 0:
flip()
if !facing_right and move_dir > 0:
flip()
#PLAYING ANIMATIONS
if grounded:
if move_dir == 0:
play_anim("idle")
else:
play_anim("walk")
else:
play_anim("jump")
#FLIPPING FUNCTION
func flip():
facing_right = !facing_right
sprite.flip_h = !sprite.flip_h
#ANIMTION FUNCTION
func play_anim(anim_name):
if anim_player.is_playing() and anim_player.current_animation == anim_name:
return
anim_player.play(anim_name)
func dash():
is_dashing = true
move_speed = 700
$Timer.start()
func _on_Timer_timeout():
can_dash = false
$dash_timer.start()
is_dashing = false
move_speed = 250
#FOR THE GHOSTING SPRITE EFFECT
func _on_ghost_timer_timeout():
if is_dashing==true:
var this_ghost = preload("res://ghost.tscn").instance()
get_parent().add_child(this_ghost)
this_ghost.position = position
this_ghost.flip_h = $Sprite.flip_h
#FOR THE GAP BETWEEN DASHES
func _on_dash_timer_timeout():
can_dash = true