Godot Version
4.3
Question
So im making a platform fighter, but when i tried to rig up the jab animation, it wouldn’t play the animation, but it would register the input.
Code :
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -500.0
const dash_speed = 800
var is_dashing = false
var can_dash = true
var dash_cooldown = 50
var jumps = 0
var attacking = false
var special_charge = 0
enum State {
IDLE,
RUN,
JUMP,
FALL,
DODGE,
JAB
}
var current_state = State.IDLE
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor() || jumps != 0:
velocity += get_gravity() * delta
dash_cooldown = dash_cooldown + 1
if dash_cooldown > 50:
dash_cooldown = 50
if is_on_floor():
current_state = State.IDLE
jumps = 0
# Handle jump.
if Input.is_action_just_pressed("jump") and (is_on_floor() || jumps != 2):
current_state = State.JUMP
velocity.y = JUMP_VELOCITY
jumps = jumps + 1
if Input.is_action_pressed("ui_left"):
$Sprite2D.scale.x = -1
if Input.is_action_pressed("ui_right"):
$Sprite2D.scale.x = 1
if velocity.y < 0 && is_dashing == false:
current_state = State.JUMP
if velocity.y > 0 && is_dashing == false:
current_state = State.FALL
if dash_cooldown == 50:
can_dash = true
else:
can_dash = false
## Mobility
if Input.is_action_just_pressed("dodge") && can_dash == true && velocity.x != 0:
current_state = State.DODGE
dash_cooldown = 0
is_dashing = true
velocity.y = 0
$DodgeTimer.start()
## Primary
## Jab
if Input.is_action_just_pressed("primary attack") && (current_state == State.IDLE) && attacking == false:
attacking = true
current_state = State.JAB
print("JAB")
$AttackTimers/JabTimer.start()
## Air Primary
## Secondary
## Special
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("Left", "Right")
if direction:
if is_dashing:
velocity.x = direction * dash_speed
else:
velocity.x = direction * SPEED
if is_on_floor():
current_state = State.RUN
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if is_on_floor():
current_state = State.IDLE
if Input.is_action_pressed("ui_left") && is_on_floor():
$Sprite2D.scale.x = -1
if Input.is_action_pressed("ui_right") && is_on_floor():
$Sprite2D.scale.x = 1
move_and_slide()
##Handle Animations
if current_state == State.IDLE:
$AnimationPlayer.play("Idle")
if current_state == State.RUN:
$AnimationPlayer.play("Run")
if current_state == State.JUMP:
$AnimationPlayer.play("Jump")
if current_state == State.FALL:
$AnimationPlayer.play("Fall")
if current_state == State.DODGE:
$AnimationPlayer.play("Dodge")
if current_state == State.JAB:
$AnimationPlayer.play("Jab")
func _on_dodge_timer_timeout() → void:
is_dashing = false
if Input.is_action_pressed(“ui_left”):
$Sprite2D.scale.x = -1
if Input.is_action_pressed(“ui_right”):
$Sprite2D.scale.x = 1
if is_on_floor():
current_state = State.IDLE
elif not is_on_floor():
current_state = State.FALL
func _on_dash_cooldown_timeout() → void:
dash_cooldown = dash_cooldown + 1
$DashCooldown.start()
func _on_super_charger_timeout() → void:
if special_charge != 100:
special_charge = special_charge + 1
print(special_charge)
$SpecialCharger.start()
func _on_jab_timer_timeout() → void:
attacking = false
current_state = State.IDLE