Syoma
Syoma

Reputation: 185

dash mechanic for 2d platformer godot 3.4

im making a 2d platformer in godot with a dash mechanic. i have already tried implementing it myself. i have put in a cool down timer for the dash(which works), made it so that cant dash while in air(which works), and animation(which doesn't work).my code has the following problems:

  1. player "teleports" rather than smoothly and quickly dashing
  2. the animation for dashing is barely visible (visible for only a frame)
  3. for some reason if you are idle(not pressing any buttons) and then push the dash button it propels you further than if you are running(holding one of the arrow keys) and push the dash button.

here is my code. it has a lot of code in it that probably isn't causing the problem but i left it in just in case it is. i put #important in front of all the parts of the code that i deemed important

extends KinematicBody2D

var vel = Vector2(0,0)
var fallswitch=0
var can_jump
var can_dash=true

const GRAVITY = 30
const SPEED = 250
const JUMPFORCE = -1000
const DASHSPEED = 1000

func _physics_process(delta):
     
#important
    if Input.is_action_pressed("dash") and is_on_floor() and can_dash==true:
        $Sprites.play("dash")
        if $Sprites.flip_h==false:
            vel.x  = DASHSPEED
        else:
            vel.x  = -DASHSPEED
        can_dash=false
        $dashcooldown.start()
            
    
    elif Input.is_action_pressed("right"):
        vel.x  = SPEED
        $Sprites.flip_h=false
        $Sprites.play("run")
    elif Input.is_action_pressed("left"):
        vel.x  = -SPEED
        $Sprites.flip_h=true
        $Sprites.play("run")
    else:
        pass
        $Sprites.play("idle")
    
    if not is_on_floor():
        if vel.y < 200 and vel.y > 0:
            $Sprites.play("fall.t")
            
        elif vel.y > 200:
            $Sprites.play("fall")
        
        else:
            $Sprites.play("air")
    
    if is_on_floor():
        can_jump=true
    elif can_jump==true and $CoyoteTimer.is_stopped()  :
        $CoyoteTimer. start()
         
        
    
    if Input.is_action_just_pressed("jump") && can_jump==true:
        vel.y = JUMPFORCE
    
    
    vel.y+=GRAVITY 
    vel=move_and_slide(vel,Vector2.UP)
    vel.x = lerp(vel.x,0,0.1)
    
 




func _on_CoyoteTimer_timeout():
    can_jump=false



        

#important
func _on_dashcooldown_timeout():
    can_dash=true

thank in advance :)

Upvotes: 0

Views: 2868

Answers (1)

Kenart
Kenart

Reputation: 335

Your code seems good. The problem might be from your camera2d. In order to implement a proper dash effect the camera needs to have a smoothing effect(it has to slowly stop when it reaches the player whenever the player moves). It acts as a lag, if there is no lag then the camera will sharply follow the player at every moment and will make a dash seem like a teleport. Try out these camera settings. In the camer2d property section enable the current property, under the limit section enable the smooth property, under the smoothing section enable the smoothing and set the speed to 8.8.

More Tips

At _on_dashcooldown_timeout(), be sure to set the player x vector component back to zero Only use one sprite frame for dash as you do not need multiple images for it, since it is short and quick.

Upvotes: 0

Related Questions