0 votes

I'm trying to use Sprite, AnimationPlayer, AnimationTree with Statemachine and inside a blendspace 2d, but I can't quite get it right and I know I'm doing something really wrong.
What I'm trying to achieve is the dash effect that I made works with animatedSprite. I can't get it right when using the nodes earlier that is said.

Here's my Code:

extends KinematicBody2D

export var MAX_SPEED = 120
export var ACCELERATION = 500
export var FRICTION = 500
export var DASH_SPEED = 230

enum{
    MOVE,
    DASH,
    ATTACK
}


var state = MOVE
var velocity = Vector2.ZERO
var dash_vector = Vector2.LEFT
var mouse_pos
var player_pos

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

func _ready():
    animationTree.active = true

func _physics_process(delta):
    match state:
        MOVE:
            move_state(delta)
        DASH:
            dash_state(delta)
        ATTACK:
            pass

func move_state(delta):
    mouse_pos = get_global_mouse_position()
    player_pos = get_position()
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("move_left") - Input.get_action_strength("move_right")
    input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
    input_vector = input_vector.normalized()

    if input_vector != Vector2.ZERO:
        dash_vector = input_vector
        animationTree.set("parameters/Idle/blend_position", input_vector)
        animationTree.set("parameters/Run/blend_position", input_vector)
        animationTree.set("parameters/Dash/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)

    move()
    if Input.is_action_just_pressed("dash_input"):
        state = DASH


func dash_state(delta):
    velocity = dash_vector * DASH_SPEED
    animationState.travel("Dash")
    move()

func move():
    velocity = move_and_slide(velocity)


func _on_Sprite_animation_finished():
    velocity = velocity * 0.8
    state = MOVE



func _on_Dash_Effect_timeout():
    if state == DASH:
        var ghostEffect = preload("res://Assets/Player/DashEffect.tscn").instance()
        get_parent().add_child(ghostEffect)
        ghostEffect.position = position
        ghostEffect.texture = animationPlayer.frames.get_frame(animationPlayer.animation, animationPlayer.frame)

And here is the code in my dash effect if you're wndering:

extends Sprite

func _ready():
    $Tween.interpolate_property(self, "modulate", Color(5, 5, 5, 1), Color(1, 1, 1, 0), 0.6, Tween.TRANS_SINE, Tween.EASE_OUT)
    $Tween.start()

func _on_Tween_tween_all_completed():
    queue_free()
Godot version latest
in Engine by (46 points)

Is this the ghost effect? if yes then you don't have to do all this trouble , I have a simple yet effective way to deal : https://youtu.be/kAMx6SUAh2s

if not then you can comment on what is your purpose

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.