how do i fix animation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bacon_Gb12

im trying to make an attack animation and i wrote this:

if Input.is_action_just_pressed("Attack"):
	$AnimationPlayer.play("Attack")

It doesn’t work for some reason, can someone help?

It can be many reasons. Please provide more information.

avnih | 2021-04-24 18:06

what info do you need?

Bacon_Gb12 | 2021-04-24 18:25

whenever I press attack it resets the idle animation and i don’t get any errors

Bacon_Gb12 | 2021-04-24 18:26

I’m guessing that it plays for one frame and then goes back to the idle animation. Can we see the rest of your code?

exuin | 2021-04-24 20:11

xtends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const MAXFALLSPEED = 300
const MAXSPEED = 300
const JUMPFORCE = 400
const ACCEL = 10

var facing_right = true

var motion = Vector2()
func _ready() → void:
pass

func _physics_process(_delta):

if Health <= 0:
	Kill()

if Input.is_action_just_pressed("Swipe"):
	$AnimationPlayer.play("Attack")

motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
	motion.y = MAXFALLSPEED
	
if facing_right == false:
	$Sprite.flip_h = true
else:
	$Sprite.flip_h = false

motion.x = clamp(motion.x,-MAXSPEED,MAXSPEED)

if Input.is_action_pressed("right"):
	motion.x += ACCEL
	facing_right = true
	$AnimationPlayer.play("Walk")
elif Input.is_action_pressed("left"):
	motion.x -= ACCEL
	facing_right = false
	$AnimationPlayer.play("Walk")
else:
	motion.x = lerp(motion.x,0,0.2)
	$AnimationPlayer.play("Idle")
	
if is_on_floor():
	if Input.is_action_just_pressed("jump"):
		motion.y = -JUMPFORCE

motion = move_and_slide(motion,UP)

if Input.is_action_just_pressed("Attack"):
	$AnimationPlayer.play("Attack")

var Health = max_health
var attack_cooldown_time = 1000
var next_attack_time = 0
var attack_damage = 30
const max_health = 100
export var Damage = 1

var collision_damage = 50
var enemy_damage = 100

func Kill():
get_tree().reload_current_scene()

func _on_Area2D_area_entered(_area: Area2D) → void:
print(“Ouch!”)
$AudioStreamPlayer.play()
Health -= 10
print(Health)

Bacon_Gb12 | 2021-04-24 20:46

:bust_in_silhouette: Reply From: exuin
else:
    motion.x = lerp(motion.x,0,0.2)
    $AnimationPlayer.play("Idle")

# ...

if Input.is_action_just_pressed("Attack"):
    $AnimationPlayer.play("Attack")

Yeah, your issue is that you only play the attack animation on the frame that the “Attack” action is pressed. The next frame, since you didn’t just press the “Attack” button, it will play the idle animation. My suggestion is to have a variable that stores whether the player is attacking or not and not play “Walk” or “Idle” when it’s true.

what do i set the variable to?

Bacon_Gb12 | 2021-04-24 22:51

and is there an easier way to do this?

Bacon_Gb12 | 2021-04-24 22:52

Set it to true when the player starts attacking and then set it to false when the player stops attacking.

exuin | 2021-04-24 22:52

And no there’s not an easier way to do this

exuin | 2021-04-24 22:53

I’m not very good with variables so I have no clue what you’re talking about

Bacon_Gb12 | 2021-04-24 22:54

and thats fine

Bacon_Gb12 | 2021-04-24 22:54

so I say
var isplayerattacking = true
?

Bacon_Gb12 | 2021-04-24 22:56

Do you have a discord? Mine is ShatteredReality#7834

exuin | 2021-04-24 22:58

yeah i have discord

Bacon_Gb12 | 2021-04-24 22:59