#I'm trying to add a dash to my 2d character but the movement overrides the dash

1 messages · Page 1 of 1 (latest)

gusty inlet
#

extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -300.0
const DASH_VELOCITY = 2000

var direction = Input.get_axis("move_left", "move_right")

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y = JUMP_VELOCITY

# Get input direction 1, 0, -1
var direction = Input.get_axis("move_left", "move_right")
# Handel dash
if Input.is_action_just_pressed("Dash") and not is_on_floor():
    velocity.x = direction * DASH_VELOCITY
    
# Flip the Sprite
if direction > 0:
    animated_sprite.flip_h =false
elif direction <0:
    animated_sprite.flip_h = true
    
# Play animations
if is_on_floor():
    if direction == 0:
        animated_sprite.play("Idle")
    else:
        animated_sprite.play("run")
else:
    animated_sprite.play("jump")
    
# Apply movement
if direction:
    velocity.x = direction * SPEED
else:
    velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()
brazen galleon
#

What if you put the movement and the dash inside true or false variables

#

This way you can control what are you executing

#

Note: I'm kinda new too, so if doesn't work Idk what to do

gusty inlet
#

Im so new I dont even really understand what you mean lol

brazen galleon
#

🤦‍♂️

gusty inlet
#

somthing kinda like this?

brazen galleon
#

Okay, it's too late, so i'm gonna sleep and tomorrow I will send you a more detailed struction

#

Okay?

gusty inlet
#

of course, no need to break your back over a random Godot noob lol

brazen galleon
#

Good night

gusty inlet
#

Good night

gusty inlet
#

extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -300.0
const DASH_VELOCITY = 1500

var direction = Input.get_axis("move_left", "move_right")

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D
@onready var timer = $Timer

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y = JUMP_VELOCITY

# Get input direction 1, 0, -1
var direction = Input.get_axis("move_left", "move_right")

var can_move = not Input.is_action_pressed("Dash")

# Handel dash
if Input.is_action_just_pressed("Dash") and not can_move and timer.is_stopped():
    velocity.x = direction * DASH_VELOCITY
    timer.start()
    
    
    
# Flip the Sprite
if direction > 0:
    animated_sprite.flip_h =false
elif direction <0:
    animated_sprite.flip_h = true
    
# Play animations
if is_on_floor():
    if direction == 0:
        animated_sprite.play("Idle")
    else:
        animated_sprite.play("run")
else:
    animated_sprite.play("jump")

# Apply movement
if direction and can_move:
    velocity.x = direction * SPEED
else:
    velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()
#

this code works

brazen galleon
#

I'm sorry

#

But I'm going in a trip today so I can't help you

#

I'm really sorry😥

#

But there may be something useful in the Godot documentation

#

If don't find nothing try there

brazen galleon
#

So forget it, if it works its fine

oblique rivet
#

Yeah a boolean is the easiest way to handle these.

#

If you need a more complex system later look into state machines. If you end up with is_walking, is_running, is_jumping etc etc. it can become overwhelming.
But for a simple game booleans are fine!

gusty inlet
brazen galleon