Godot Version
4.2.1.stable
Question
I’ve added a gunshot animation in each direction, but I am having 2 problems. One is that the animation is cut off unless the input key is held. The other is that the animation will loop if the input key is held. Any help is appreciated
extends CharacterBody2D
@export var speed = 150
@onready var anim = $player_sprite
@onready var gunshot = $gunshot_sound
var direction = “down”
var is_shooting: bool = false
var last_anim_direction: String = “down”
func get_input():
var input_direction = Input.get_vector(“move_left”, “move_right”, “move_up”, “move_down”)
velocity = input_direction * speed
if Input.is_action_pressed("shoot"):
anim.play("shoot_" + last_anim_direction)
is_shooting = true
await anim.animation_finished
if Input.is_action_just_released("shoot"):
is_shooting = false
func update_animation():
if is_shooting: return
var is_moving = velocity.length_squared() > 0
if is_moving:
if velocity.x < 0: direction = "left"
elif velocity.x > 0: direction = "right"
elif velocity.y > 0: direction = "down"
elif velocity.y < 0: direction = "up"
anim.play("walk_" + direction)
else:
anim.play("idle_" + direction)
last_anim_direction = direction
func update_sound_fx():
if is_shooting:
gunshot.play()
else:
gunshot.stop()
func _physics_process(_delta):
get_input()
move_and_slide()
update_animation()
update_sound_fx()