Godot Version
4.3
Question
howdy,
i can’t get the “dash” animation for my 2d sidescrolling game i’m learning with to work.
here’s the code i have for the dash.
variables:
var dash_cooldown = 0.5
var dash = false
this in a input function:
if Input.is_action_just_pressed("dash") and velocity.x and not $Timers/DashCooldown.time_left:
dash = true
$Timers/DashCooldown.start()
this in a movement function:
if dash:
dash = false
var dash_tween = create_tween()
dash_tween.tween_property(self, 'velocity:x', velocity.x + direction.x * 300, 0.2)
dash_tween.connect("finished", on_dash_finished)
gravity_multiplier = 0
and this function right here:
func on_dash_finished():
velocity.x = move_toward(velocity.x, 0, 500)
gravity_multiplier = 1
all of this is from a tutorial i’m following on youtube.
for all the other animations like running, jumping, idle and what not i have a animation function where i’m like “if this and that is pressed, play animation” but that does not work for the dash somehow.
can someone eli5 how this works?
thanks in advance!
Do you have other code that edits the velocity.x
? Could you show more of the script, including how you handle movement?
for sure, this is the complete code:
extends CharacterBody2D
#movement
var direction = Vector2.ZERO
var speed = 80
var acceleration = 1000
var friction = 900
var dash_cooldown = 0.5
var dash = false
var ducking = false
var can_move = true
# jumping
var jump_strength = 240
var gravity = 900
var terminal_velocity = 500
var wall_sliding_speed = 1200
var wall_jump_push = 120
var wall_jump_strength = 100
var gravity_multiplier = 1
var jump = false
var faster_fall = false
func _ready():
$Timers/DashCooldown.wait_time = dash_cooldown
func _process(delta):
apply_gravity(delta)
if can_move:
get_input()
apply_movement(delta)
player_animation()
func get_input():
# horizontal movement
direction.x = Input.get_axis("left", "right")
# jump
if Input.is_action_just_pressed("jump"):
# normal
if is_on_floor() or $Timers/Coyote.time_left:
jump = true
# wall jump
if is_on_wall_only() and Input.is_action_pressed("left"):
velocity.x = wall_jump_push
velocity.y = wall_jump_strength
jump = true
elif is_on_wall_only() and Input.is_action_pressed("right"):
velocity.x = -wall_jump_push
velocity.y = wall_jump_strength
jump = true
# buffer
if velocity.y > 0 and not is_on_floor():
$Timers/JumpBuffer.start()
if Input.is_action_just_released("jump") and not is_on_floor() and velocity.y < 0:
faster_fall = true
# dash
if Input.is_action_just_pressed("dash") and velocity.x and not $Timers/DashCooldown.time_left:
dash = true
$Timers/DashCooldown.start()
# ducking
ducking = Input.is_action_pressed("duck") and is_on_floor()
#print(ducking)
func apply_movement(delta):
# wall slide
if is_on_wall_only() and velocity.y >= 0 and (Input.is_action_pressed("left") or Input.is_action_pressed("right")): velocity.y = wall_sliding_speed * delta
# left / right movement
if direction.x:
velocity.x = move_toward(velocity.x, direction.x * speed, acceleration * delta)
else:
velocity.x = move_toward(velocity.x, 0, friction * delta)
# ducking
if ducking:
velocity.x = 0
# jumping
if jump or $Timers/JumpBuffer.time_left and is_on_floor():
velocity.y = -jump_strength
jump = false
faster_fall = false
var on_floor = is_on_floor()
move_and_slide()
# coyote
if on_floor and not is_on_floor() and velocity.y >= 0:
$Timers/Coyote.start()
# dash
if dash:
dash = false
var dash_tween = create_tween()
dash_tween.tween_property(self, 'velocity:x', velocity.x + direction.x * 300, 0.2)
dash_tween.connect("finished", on_dash_finished)
gravity_multiplier = 0
func apply_gravity(delta):
velocity.y += gravity * delta
velocity.y = velocity.y / 2 if faster_fall and velocity.y < 0 else velocity.y
velocity.y = velocity.y * gravity_multiplier
velocity.y = min(velocity.y, terminal_velocity)
func on_dash_finished():
velocity.x = move_toward(velocity.x, 0, 500)
gravity_multiplier = 1
func player_animation():
# idle
if !Input.is_anything_pressed() and is_on_floor():
$Sprite2D.play("idle")
$Effects/RunningParticles.emitting = false
# running
if is_on_floor():
if Input.is_action_pressed("left"):
$Sprite2D.flip_h = true
$Sprite2D.play("run")
$Effects/RunningParticles.emitting = true
$Effects/RunningParticles.process_material.gravity = Vector3(5,-50,0)
$Effects/RunningParticles.position.x = 5
elif Input.is_action_pressed("right"):
$Sprite2D.flip_h = false
$Sprite2D.play("run")
$Effects/RunningParticles.emitting = true
$Effects/RunningParticles.process_material.gravity = Vector3(-5,-50,0)
$Effects/RunningParticles.position.x = -5
# jump up
if Input.is_action_pressed("jump"):
if Input.is_action_pressed("right"):
$Sprite2D.flip_h = false
$Sprite2D.play("jump_up")
elif Input.is_action_pressed("left"):
$Sprite2D.flip_h = true
$Sprite2D.play("jump_up")
else:
$Sprite2D.play("jump_up")
# jump down
if velocity.y > 0:
if Input.is_action_pressed("right"):
$Sprite2D.flip_h = false
$Sprite2D.play("jump_down")
elif Input.is_action_pressed("left"):
$Sprite2D.flip_h = true
$Sprite2D.play("jump_down")
else:
$Sprite2D.play("jump_down")