Every time the player spawns in the starting position, the weapon is in hand. Only when I press my attack does the weapon disappear.
I’m still new to Godot, so I need help understanding the logic here. Is this an issue with my animation?
My weapon.gd script
extends Node2D
var weapon: Area2D
func _ready():
if get_children().is_empty(): return
weapon = get_children()[0]
func enable():
if !weapon: return
visible = true
weapon.enable()
func disable():
if !weapon: return
visible = false
weapon.disable
My player.gd script
extends CharacterBody2D
signal healthChanged
@export var speed: int = 35
@onready var animations = $AnimationPlayer
@onready var effects = $Effects
@onready var hurtBox = $hurtBox
@onready var hurtTimer = $hurtTimer
@onready var weapon = $weapon
@onready var death = $death
@export var maxHealth = 3
@onready var currentHealth: int = maxHealth
@export var knockbackPower: int = 500
# Assuming Inventory is another class
@export var inventory: Inventory
var lastAnimDirection: String = "Down"
var isHurt: bool = false
var isAttacking: bool = false
func _ready():
effects.play("RESET")
func handleInput():
var moveDirection = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = moveDirection * speed
if Input.is_action_pressed("attack"):
attack()
func attack():
animations.play("attack" + lastAnimDirection)
isAttacking = true
weapon.enable()
await animations.animation_finished
weapon.disable()
isAttacking = false
func updateAnimation():
if isAttacking: return
if velocity.length() == 0:
if animations.is_playing():
animations.stop()
else:
var direction = "Down"
if velocity.x < 0: direction = "Left"
elif velocity.x > 0: direction = "Right"
elif velocity.y < 0: direction = "Up"
animations.play("walk" + direction)
lastAnimDirection = direction
func handleCollision():
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
var collider = collision.get_collider()
print_debug(collider.name)
func _physics_process(delta):
handleInput()
move_and_slide()
handleCollision()
updateAnimation()
if !isHurt:
for area in hurtBox.get_overlapping_areas():
if area.name == "hitBox":
hurtByEnemy(area)
func hurtByEnemy(area):
currentHealth -= 1
if currentHealth <= 0:
die() # Call the die function
healthChanged.emit(currentHealth)
isHurt = true
knockback(area.get_parent().velocity)
effects.play("hurtBlink")
hurtTimer.start()
await hurtTimer.timeout
effects.play("RESET")
isHurt = false
func die():
# Reset character state upon death
currentHealth = maxHealth
# Additional reset logic
healthChanged.emit(currentHealth)
position = Vector2(0, 0)
velocity = Vector2.ZERO
# Play death animation or effects if desired
animations.play("death")
effects.play("deathEffect")
func _on_hurt_box_area_entered(area):
if area.has_method("collect"):
area.collect(inventory)
func knockback(enemyVelocity: Vector2):
var knockbackDirection = (enemyVelocity - velocity).normalized() * knockbackPower
velocity = knockbackDirection
move_and_slide()
func _on_hurt_box_area_exited(area): pass
I was expecting the player to start with hands empty.
New contributor
Fadeh Baghomian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.