Godot Version
GODOT 4
Question
I’m trying to implement a dash to my game.
I want this dash to make the velocity equal to 0 when it’s finished. (A bit like celeste)
But, when i do this, the gravity doesn’t work as usual.
when falling, after reaching a certain velocity.y, the velocity.y put itself to 0, making this multiple times until my character reaches the ground.
I don’t know why this happens.
Since I can’t post videos yet, i’ll just show you the code:
extends CharacterBody2D
const SPEED = 200.0
const DASH_SPEED = 900
const JUMP_VELOCITY = -700.0
const GRAVITY = 1500
var MAX_DASH = 1
var DASH_COUNTER = MAX_DASH
var DASH_DIRECTION = Vector2(0,0)
var IS_DASHING = false
var DASH_JUST_FINISHED = false
var JOYSTICK_DEADZONE
@onready var anim = $Brick_Main_Character_Sprite
func _physics_process(delta: float) -> void:
# Add the gravity.
if is_on_floor() and not IS_DASHING:
DASH_COUNTER = MAX_DASH
if not is_on_floor() and not IS_DASHING and not DASH_JUST_FINISHED:
velocity.y += GRAVITY * delta
# Handle jump.
if Input.is_action_just_pressed("JumpAction") and is_on_floor():
velocity.y = JUMP_VELOCITY
print(velocity.y, get_gravity(), delta)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var directionx := Input.get_axis("LeftMovement", "RightMovement")
var directiony := Input.get_axis("DownMovement", "UpMovement")
if IS_DASHING:
if DASH_DIRECTION.x != 0 and DASH_DIRECTION.y != 0:
velocity.x = DASH_SPEED/sqrt(2) * DASH_DIRECTION.x
velocity.y = -DASH_SPEED/sqrt(2) * DASH_DIRECTION.y
else:
velocity.x = DASH_SPEED * DASH_DIRECTION.x
velocity.y = -DASH_SPEED * DASH_DIRECTION.y
elif directionx:
velocity.x = directionx/abs(directionx) * SPEED
anim.play("Running")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
anim.play("Idle")
if DASH_JUST_FINISHED:
velocity.y = move_toward(velocity.y, 0, 1000)
DASH_JUST_FINISHED = false
#DASH INPUT HANDLE
if Input.is_action_just_pressed("DashAction") and DASH_COUNTER > 0:
if directionx >= 0.2:
DASH_DIRECTION.x = 1
elif directionx > -0.2:
DASH_DIRECTION.x = 0
else:
DASH_DIRECTION.x = -1
if directiony >= 0.2:
DASH_DIRECTION.y = 1
elif directiony > -0.2:
DASH_DIRECTION.y = 0
else:
DASH_DIRECTION.y = -1
IS_DASHING = true
DASH_COUNTER -= 1
$Brick_Main_Character_Dash_Timer.start()
#SPRITE FLIP
if directionx < 0:
anim.flip_h = true
elif directionx > 0:
anim.flip_h = false
move_and_slide()
func _on_brick_main_character_dash_timer_timeout() -> void:
IS_DASHING = false
DASH_JUST_FINISHED = true