Hi im trying to do some kind of vampire survivors clone for learning purposes and i have a little problem with my shooting mechanic. So basically right now my weapon uses timer to shoot arrows towards enemies but i want it to shoot only if there is enemy in range and i cant figure out how to make it work. My code so far looks like this:
extends Area2D
func _physics_process(delta: float) -> void:
var enemies_in_range = get_overlapping_bodies()
if enemies_in_range.size() > 0:
var target_enemy = enemies_in_range[0]
look_at(target_enemy.global_position)
func shoot():
const ARROW = preload("res://arrow.tscn")
var new_arrow = ARROW.instantiate()
new_arrow.global_position = %ShootingPoint.global_position
new_arrow.global_rotation = %ShootingPoint.global_rotation
%ShootingPoint.add_child(new_arrow)
func _on_timer_timeout() -> void:
shoot()
I would be glad if someone could explain to me what I need to do in steps that a total beginner could understand(or look it up on youtube/google). Thanks!