Logo

dev-resources.site

for different kinds of informations.

Godot4 2D: Enemy Spawn Radious - problem with spawning enemy on player, no infinity loops

Published at
9/1/2024
Categories
godot
gdscript
gamedev
2d
Author
malybaryl
Categories
4 categories in total
godot
open
gdscript
open
gamedev
open
2d
open
Author
9 person written this
malybaryl
open
Godot4 2D: Enemy Spawn Radious - problem with spawning enemy on player, no infinity loops

Hi!

Below is my Godot 4 code that addresses the problem of enemies spawning inside the player. The code is safe from infinite loops and reliably returns the x and y positions where an enemy should spawn. The spawn radius dynamically adjusts based on the player's scale, and you can set both minimum and maximum distances for the enemy's spawn position.

Feel free to discuss if you have better solutions or improvements. This code was used in my game Evolution Conquer, and the full open-source code can be found on my GitHub: evolution-conquer-source-code.

Required Nodes:

Area2D: The script should be attached to an Area2D node.

SpawnRadious: A child node of type Area2D, with a CircleShape2D to define the spawn radius.

You will need to implement your own enemy generation logic. Additionally, itโ€™s advisable to handle cases where the enemy spawns outside the game boundaries, such as by ensuring the enemy moves back towards the playable area.

extends Area2D

# spawn_radius & spawn_center values here are only
# placeholders, they will change from collision shape radious
# and player position
var spawn_radius = 1000
var spawn_center = Vector2(0, 0)

# spawn_radious < x < spawn_radious_limitation 
# x - position where enemy will apear
@export var spawn_radious_limitation = 2000

var parent
var spawn_radious_node
var spawn_radious_node_shape

func _ready():
    parent = get_parent() as Node2D
    spawn_radious_node = get_node("SpawnRadious")
    spawn_radious_node_shape = spawn_radious_node.shape as CircleShape2D
    spawn_radius = spawn_radious_node_shape.radius * parent.scale.x

func get_spawn_position():
    # sometimes is a trouble with getting parent in _ready() funcion
    if parent == null:
        parent = get_parent() as Node2D
        spawn_radious_node = get_node("SpawnRadious")
        spawn_radious_node_shape = spawn_radious_node.shape as CircleShape2D
        spawn_radius = spawn_radious_node_shape.radius * parent.scale.x

    # getting player position
    var spawn_center = parent.position
    print("spawn center: ", spawn_center)
    print("spawn radious: ", spawn_radius)

    # calculation enemy position
    var angle = randf_range(0, PI * 2)
    var distance = randf_range(spawn_radius, spawn_radius + spawn_radious_limitation) 

    var position_x = spawn_center.x + cos(angle) * distance
    var position_y = spawn_center.y + sin(angle) * distance

    var spawn_position = Vector2(position_x, position_y)
    print("spawn_position: ", spawn_position)

    return spawn_position

Enter fullscreen mode Exit fullscreen mode
godot Article's
30 articles in total
Favicon
endless runner in godot 4 3d all systems and minus like subway surfers for mobile
Favicon
How to Customize Input Bindings in Godot
Favicon
Seamless Inter-Process Communication with Godot's `execute_with_pipe`.
Favicon
How a indie game developer should follow the discipline of game development?
Favicon
2D Game Menu with Godot4
Favicon
Menu de Game Retrรด com Godot4
Favicon
Unlocking the Power of Gaming with Game Vault: A Valuable Resource for the DEV Community
Favicon
Launching my first game soon!
Favicon
Strontium | The Ultimate Portfolio App for Gamers and Indie Game Developers
Favicon
The Big Refactoring - Chapter 0
Favicon
๐Ÿ‡ซ๐Ÿ‡ท Framework Heroes News : la veille sur les frameworks pour la semaine 2024/40
Favicon
5 WAYS TO ORGANIZE YOUR C# CODES IN GODOT
Favicon
Introducing Mineral Hunt Mode: A Game-Changing Experience in Narqubis
Favicon
Behind the Scenes: Designing a Beat Saber-Style Game with Godot
Favicon
Basics of Game Development Using Unity, Unreal Engine, or Godot
Favicon
Unity vs. Godot: A Game Developer's Guide
Favicon
Beach Buggy Racing Mod Apk
Favicon
Online Visual Novel in Godot: Case Study on Sentou Gakuen
Favicon
Godot4 2D: Enemy Spawn Radious - problem with spawning enemy on player, no infinity loops
Favicon
Godot 3D Multiplayer Template: A Starting Point for Creating 3D Multiplayer Games
Favicon
WordPress Co-Founder Matt Mullenweg Declares WP Engine a 'Cancer' โ€“ Is Your Hosting Provider Hurting the Community?
Favicon
aus new adn cool
Favicon
How to Press and Drag to Reposition a Window in Godot
Favicon
Learn By Example: Bash Script - Godot 4 Project Creator
Favicon
"Surf the Rails in Subway Surfers Online"
Favicon
I Made A Plugin To Update Godot From Within The Editor
Favicon
7 Key Reasons Why Price Localization is Crucial for Global Success
Favicon
If Youโ€™re Interested In Learning Game Development, Subscribe To These 3 YouTube Channels
Favicon
Godot Rust CI: Handy GDScript & Rust GitHub Actions
Favicon
How Corporate Greed Killed the Joy of Gaming for Gamers Worldwide

Featured ones: