I have searched for a solution for hours. I found some info that addresses this issue although it is limited and outdated for Godot4. There are many tutorials on creating enemies, but they all seem to skip over how to actually set spawn locations for enemies. I used the code as described in the Godot 2D tutorial and no matter what I try the enemies will only spawn at the top left corner and they appear to get confused (hung up) before attempting to enter the screen at all. I want them to spawn at random locations within the game screen or at least a hard point location in the center of the screen. Not sure what I'm missing here.

  • xyz replied to this.
    func _on_mob_timer_timeout():
    	var mob = mob_scene.instantiate() 
    	var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
    	mob_spawn_location.progress_ratio = randf()
    	var direction = mob_spawn_location.rotation + PI / 2 
    	mob.position = mob_spawn_location.position 
    	direction += randf_range(-PI / 4, PI / 4)
    	mob.rotation = direction 
    	var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
    	mob.linear_velocity = velocity.rotated(direction)
    	add_child(mob)
    • xyz replied to this.

      Spindraft
      According to your code snippet, every spawned enemy copies the position of MobSpawnLocation node. Since your code doesn't change the position of that node, all enemies will appear at the same exact location.

      I thought the mob_spawn_location.progress_ratio = randf() was supposed to handle that. I would be happy to hard wire spawn locations into the code but I'm not sure how to do that yet. I have been searching for code tutorials etc for days and can't seem to find what I need to move forward with manipulating the starting location of enemies.

      • xyz replied to this.

        Spindraft I thought the mob_spawn_location.progress_ratio = randf() was supposed to handle that.

        What probably handles it is the script attached to MobSpawnLocation. That line just changes some property in that script. But it's hard to tell what exactly happens without seeing the actual script.