Godot Version
Godot_v4.2-stable_win64
Question
Hello everyone. I’ve been racking my brains for two days, hoping for your help.
- The falling animation doesn’t allow a second jump and dash, while playing
- The walking animation continues if you press the button and hold the direction button. It only turns on while moving.
- The jumping animation (or falling, if you turn it on) near the wall and on the floor loops
extends CharacterBody2D
enum {
IDLE,
WALKING,
MOVE,
JUMP,
JUMP2,
FALL,
}
const WALK = SPEED/6
const DASH_SPEED = 1200
const SPEED = 600.0
@export var jump_height : float
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
@onready var jump_velocity : float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0
@onready var jump_gravity : float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
@onready var fall_gravity : float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0
@onready var anim = $AnimatedSprite2D
@onready var animPlayer = $AnimationPlayer
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
var jump_number = 2
var state = MOVE
var dashing = false
var can_dash = true
var jumping = true
func _physics_process(delta: float) -> void:
match state:
MOVE:
move_state()
WALKING:
walking_state()
JUMP:
jump_state()
JUMP2:
jump2_state()
FALL:
fall_state()
DASH:
dash_state()
move_and_slide()
if not is_on_floor(): velocity.y += get_gravity() * delta
Global.player_pos = self.position
#if velocity.y > 0:
#state = FALL
#elif velocity.y < 0:
#if jump_number > 0:
#state = JUMP
#else:
#state = JUMP2
func move_state ():
var direction := Input.get_axis("left", "right")
if direction == -1:
$AnimatedSprite2D.flip_h = true
elif direction == 1:
$AnimatedSprite2D.flip_h = false
if direction:
if dashing:
stats.stamina_cost = stats.dash_cost
velocity.x = direction * DASH_SPEED
else:
velocity.x = direction * SPEED
if velocity.y == 0 and is_on_floor_only():
animPlayer.play("Run")
else:
velocity.x = 0
if velocity.y == 0:
animPlayer.play("Idle")
if Input.is_action_just_pressed("Dash") and direction:
velocity.x = direction * DASH_SPEED
state = DASH
if direction and Input.is_action_just_pressed("Walk") and is_on_floor_only():
velocity.x = direction * WALK
state = WALKING
#if velocity.y != 0: #and !Input.is_action_just_pressed("Dash") or !Input.is_action_just_pressed("Jump2"):
#state = FALL
if Input.is_action_just_pressed("Dash") and can_dash and stats.stamina > stats.stamina_cost:
dashing = true
can_dash = false
$DashTimer.start()
$DashTimer/DashAgain.start()
if jumping:
if is_on_floor():
jump_number = 2
if Input.is_action_just_pressed("Jump") and is_on_floor():
stats.stamina_cost = stats.jump_cost
velocity.y = jump_velocity
state = JUMP
if Input.is_action_just_pressed("Jump") and is_on_floor() or Input.is_action_just_pressed("Jump") and jump_number > 0 and stats.stamina > stats.stamina_cost:
velocity.y = jump_velocity
jump_number = jump_number - 1
state = JUMP2
func walking_state():
animPlayer.play("Walking")
if Input.is_action_just_released("Walk"):
animPlayer.play("Idle")
state = MOVE
func jump_state():
animPlayer.play("Jump")
await animPlayer.animation_finished
state = MOVE
func jump2_state():
animPlayer.play("Jump")
#await animPlayer.animation_finished
state = MOVE
func fall_state():
animPlayer.play("Fall")
#await animPlayer.animation_finished
state = MOVE