1
\$\begingroup\$

So, I'm trying to make a character do a wavedash in Godot 3 and I don't know what I'm missing here. When I press 'Z', the character should burst speed and then a few miliseconds later it should decelerate back to the normal speed. Tried to use a while loop, adding the var to the motion, but it didn't work.

Here's the code (testing only in the right direction).

extends KinematicBody2D
const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
var MAX_SPEED = 150
var JUMP_HEIGHT = -400
var MAX_DSPEED = 300
var motion = Vector2()
var CAN_DASH = true
var ADD_SPEED = 0

func _physics_process(delta):

motion.y += GRAVITY
var friction = false

if Input.is_action_pressed("ui_right"):
    motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
    $Sprite.flip_h = false
    $Sprite.play("Run")
    if Input.is_action_pressed("dash"):
        ADD_SPEED = 300
        while ADD_SPEED > 0:
            ADD_SPEED -= 3
            motion.x = min(motion.x+ADD_SPEED, MAX_DSPEED)

elif Input.is_action_pressed("ui_left"):
    motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
    $Sprite.flip_h = true
    $Sprite.play("Run")
else:
    $Sprite.play("Idle")
    friction = true
    motion.x = lerp(motion.x, 0, 0.2)
    JUMP_HEIGHT = -400

if is_on_floor():
    if Input.is_action_just_pressed("ui_up"):
        motion.y = JUMP_HEIGHT
    if friction == true:
        motion.x = lerp(motion.x, 0, 0.2)
else:
    if motion.y < 0:
        $Sprite.play("Jump")
    else:
        $Sprite.play("Fall")
    if friction == true:
        motion.x = lerp(motion.x, 0, 0.05)
motion = move_and_slide(motion, UP)
pass
\$\endgroup\$
1
\$\begingroup\$

You can't use a while loop in this case. When you execute your script and press the dash button you while loop is processed before the next rendering or physics step. That means you just change you motion.x value 100 times until the add_speed variable has a value of 0.

Since you use the add_speed only for the dash, I think the easiest way to perform the dash with your code is to reduce the add_speed in each call of the physics_process function. For example by adding the following line before(!) the if Input.is_action_pressed("ui_right"): part:

if add_speed > 0:
    add_speed -= 3

However, this would reduce the speed in a linear way. Thats fine if you want it. But if just want a really short "jump" I would consider to define a "dash state" of you character. In my opinion that would help to fine-tune the character behaviour during the dash.

\$\endgroup\$
  • \$\begingroup\$ I would also recommend using a dedicated dash state. I have had success with this method in the past. \$\endgroup\$ – GoldenGremlin Mar 9 '18 at 13:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.