0 votes

extends KinematicBody2D

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

enum {
MOVE,
ROLL,
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)

    ROLL:
        pass

    ATTACK:
        move_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):
animationstate.travel("Attack")

(note: my key for "attack" is spacebar, and j)

Godot version 3.2.3
in Engine by (12 points)

You have move_state(delta) under both MOVE and ATTACK. The attack_state(delta) function isn't being called anywhere.

If I take out move_state(delta) under MOVE, I lose the ability to access my movement animation, and can't move as well.

You can add a second argument like this move_state(delta, play_anim:= true) and under ATTACK use move_state(delta, false) and put all the animation changes under if play_anim:. However I can't see anywhere that resets state to MOVE, this can be done by connecting the animation_finished signal.

Please log in or register to answer this question.

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.