With the timer you have pretty much done the work of spawning every N seconds, actually.
The position is hardcoded, so every new enemy will appear at the same place.
First, make sure your main 2D node (the root of the scene, I assume?) has a position of (0, 0)
. It's important so there is no weird offset going on with the contents you spawn with global coordinates (or you could make the root a simple Node
, instead of Node2D
).
Next, in order to spawn enemies along the X axis of the player, you need to access the player. Then, add a distance to the X position, negative to be on the left, positive to be on the right.
Something like this?
var Enemy = preload ("res://NPC.tscn")
func _on_EnemyTimer_timeout():
var player = get_node("Path/To/Player")
var e = Enemy.instance()
var pos = player.position
if randf() < 0.5:
# On the left
pos.x -= rand_range(50.0, 200.0)
else:
# On the right
pos.x += rand_range(50.0, 200.0)
e.position = pos
add_child(e)