- Edited
Making a 2D platformer and I'm trying to set up grounded attacks in such a way that the player has to basically commit to them and let the attack animation play through before they're able to do something else, such as run or jump. The only other action they should be able to do is another attack in a combo chain if there's one available. If they're walking or running, the attack should take priority and stop the movement until it's finished.
So far I've been able to prevent the player from crouching if they're doing a standing attack, but they can still move and jump. The attack animation will play through at least, but they shouldn't be able to receive any input until after the attack(s) is finished.
Here's my code:
extends KinematicBody2D
#VARIABLES
export var walkSpeed: int = 1200;
export var runSpeed: int = 2000;
export var jumpForce: int = 2750;
export var gravity: int = 4500;
export var maxFallSpeed: int = 2750;
export var health: int = 20;
#CHECKS
var isCrouching: bool = false;
var isAttacking: bool = false;
var landing: bool = false;
var stateMachine;
var velocity: = Vector2();
func _ready():
stateMachine = $AnimationTree.get("parameters/playback");
func _process(_delta):
pass;
func input():
var current = stateMachine.get_current_node();
#MOVEMENT
if Input.is_action_pressed("right") and !isCrouching and !isAttacking:
$Sprite.scale.x = 1;
if Input.is_action_pressed("run"):
velocity.x = runSpeed;
if is_on_floor():
stateMachine.travel("Run");
else:
velocity.x = walkSpeed;
if is_on_floor():
stateMachine.travel("Walk");
elif Input.is_action_pressed("left") and !isCrouching and !isAttacking:
$Sprite.scale.x = -1;
if Input.is_action_pressed("run"):
velocity.x = -runSpeed;
if is_on_floor():
stateMachine.travel("Run");
else:
velocity.x = -walkSpeed;
if is_on_floor():
stateMachine.travel("Walk");
else:
velocity.x = 0;
if !isCrouching and is_on_floor():
stateMachine.travel("IdleStand");
#JUMP
if is_on_floor() and Input.is_action_just_pressed("jump") and !isAttacking:
velocity.y = -jumpForce;
stateMachine.travel("Jump");
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y *= 0.6;
#FALL
if velocity.y >= 0 and !is_on_floor() and !isAttacking:
stateMachine.travel("Fall");
if is_on_floor():
if landing:
$LandingParticle.restart();
$LandingParticle.emitting = true;
landing = false;
else:
if !landing:
landing = true;
#CROUCH
if is_on_floor() and Input.is_action_pressed("crouch") and velocity.y == 0 and !isAttacking:
isCrouching = true;
$CrouchCollision.disabled = false;
$BodyCollision.disabled = true;
stateMachine.travel("Crouch");
else:
isCrouching = false;
$CrouchCollision.disabled = true;
$BodyCollision.disabled = false;
#ATTACKS
if Input.is_action_just_pressed("light_attack") and is_on_floor() and !isCrouching and !isAttacking and velocity.y == 0:
isAttacking = true;
stateMachine.travel("AttackLight1");
else:
isAttacking = false;
func _physics_process(delta):
input();
velocity.y += gravity * delta;
if velocity.y > maxFallSpeed:
velocity.y = maxFallSpeed;
velocity = move_and_slide(velocity, Vector2.UP);
I have an isAttacking bool that's supposed to be checked under the movement and jump code, but it doesn't seem to work there.
Also using an AnimationTree state machine that's setup properly as far as I can tell. Pretty sure it has to do with the code, but I could be wrong.