0 votes

Ok, so in something I only recently began working on, I have quite a simple problem that I can't seem to find a solution to; the attack animation plays a second or two after I actually press the button.

Here is my code -

extends KinematicBody2D

const ACCELERATION = 500
const MAX_SPEED = 80
const FRICTION = 500

enum {
MOVE,
SLIDE,
ATTACK
}

var state = MOVE
var velocity = Vector2.ZERO

onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")

func _ready():
animationTree.active = true

func physicsprocess(delta):
match state:
MOVE:
move_state(delta)

    SLIDE:
        pass

    ATTACK:
        attack_state(delta)

func movestate(delta):
var input
vector = Vector2.ZERO
inputvector.x = Input.getactionstrength("uiright") - Input.getactionstrength("uileft")
input
vector.y = Input.getactionstrength("uidown") - Input.getactionstrength("uiup")
inputvector = inputvector.normalized()

if input_vector != Vector2.ZERO:
    animationTree.set("parameters/Idle/blend_position", input_vector)
    animationTree.set("parameters/Run/blend_position", input_vector)
    animationTree.set("parameters/Attack/blend_position", input_vector)
    animationState.travel("Run")
    velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
    animationState.travel("Idle")
    velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

velocity = move_and_slide(velocity)

if Input.is_action_just_pressed("attack"):
    state = ATTACK

func attack_state(delta):
velocity = Vector2.ZERO
animationState.travel("Attack")

func attackanimationfinished():
state = MOVE

in Engine by (15 points)

1 Answer

0 votes

Hi,
it might not be your code, Check the animation state tranistions. You want to change to the Attack state immediate when switching to it.

There are many types of transitions:

  • Immediate: Will switch to the next state immediately. The current
    state will end and blend into the beginning of the new one.
  • Sync:
    Will switch to the next state immediately, but will seek the new
    state to the playback position of the old state.
  • At End: Will wait for the current state playback to end, then switch to the beginning of the next state animation.
by (1,861 points)

All of them are set to immediate, sadly.

How about moving the input to before you change the state?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.