Hiii, so I’m stucked where I need to spawn enemies from literally any direction and have no idea how to do it. This is the current script, but this makes it only go from side to side like a dvd. And also here are the player and game codes:
Enemy code:
extends Node2D
var speed = 100
var direction = Vector2(1, 0)
@onready var sprite_2d: AnimatedSprite2D = $Sprite2D
func _ready():
set_direction()
func set_direction():
var random_direction = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized()
direction = random_direction
func _process(delta):
position += direction * speed * delta
if speed > 0 and sprite_2d.animation != 'move':
sprite_2d.animation = 'move'
if speed == 0 and sprite_2d.animation != 'idle':
sprite_2d.animation = 'idle'
if position.x > 800:
position.x = 800
direction.x = -direction.x
if position.x < 0:
position.x = 0
direction.x = -direction.x
if position.y > 600:
position.y = 600
direction.y = -direction.y
if position.y < 0:
position.y = 0
direction.y = -direction.y
Player code:
extends Node2D
var speed = 300
func _process(delta):
var direction = Vector2.ZERO
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_down"):
direction.y += 1
if Input.is_action_pressed("ui_up"):
direction.y -= 1
position += direction.normalized() * speed * delta
Game code:
extends Node2D
var player_area
var enemy_area
func _ready():
player_area = $Player/Area2D
enemy_area = $Enemy/Area2D
player_area.monitoring = true
enemy_area.monitoring = true
player_area.connect("area_entered", Callable(self, "_on_player_collision"))
enemy_area.connect("area_entered", Callable(self, "_on_enemy_collision"))
func _on_player_collision(area: Area2D):
if area == enemy_area:
get_tree().paused = true
func _on_enemy_collision(area: Area2D):
if area == player_area:
get_tree().paused = true
I use godot 4.3 and I tried the Dodge the Creeps code but it had many issues. Can anyone please help?