How can I spawn an enemy in the centre and then move out them to hit the player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iamrifki

I have a game that looks like this
enter image description here

I want to spawn the enemy mob in the middle of the screen, and then go try to hit the player, if they miss, they go offscreen and get deleted.

E.g.
enter image description here

How can I do this with my current code?

Mob.gd

extends RigidBody2D


export var speed = 250
var velocity = Vector2(1, 0)

func _process(delta):
	global_position += velocity.rotated(rotation) * speed * delta

func _ready():
var mob_types = $AnimatedSprite.frames.get_animation_names()
$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

func _on_VisibilityNotifier2D_screen_exited():
 queue_free()

Main.gd (The “Arena” or “Stage” Scene)

extends Node2D

export var speed = 150
var velocity = Vector2()
var screen_size  # Size of the game window.
var center

var mob = preload("res://Scenes/Mob.tscn")

func _ready():
	randomize()

	screen_size = get_viewport_rect().size
	center = screen_size * 0.5

func _process(delta):
	velocity.x = int(rand_range(0, 1))
	velocity.y = int(rand_range(0, 1))

	velocity = velocity.normalized()

	global_position += speed * velocity * delta

	Global.instance_node(mob, global_position, get_parent())

Global.gd

extends Node

func instance_node(node, location, parent):
	var node_instance = node.instance()
	parent.add_child(node_instance)
	node_instance.global_position = location
	return node_instance
:bust_in_silhouette: Reply From: ramazan

Look here. Set bullets as enemies

https://kidscancode.org/godot_recipes/2d/2d_shooting/

/////
If the enemy will follow the player
https://www.davidepesce.com/2019/10/14/godot-tutorial-5-1-dragging-player-with-mouse/

Doesn’t seem to work sadly. Perhaps my timers are broken?
enter image description here

Main.gd

extends Node2D

export var speed = 150
export (PackedScene) var Mob

var velocity = Vector2()

var score

func _ready():
	randomize()

func _process(delta):
	pass

func game_over():
	$ScoreTimer.stop()
	$MobTimer.stop()

func new_game():
	score = 0
	$Player.start($StartPosition.position)
	$StartTimer.start()


func _on_MobTimer_timeout():
	var mob = Mob.instance()
	owner.add_child(mob)
	mob.transform = $HoleFromHell.global_transform


func _on_ScoreTimer_timeout():
	score += 1


func _on_StartTimer_timeout():
	$MobTimer.start()
	$ScoreTimer.start()

Mob.gd

extends Area2D


export var speed = 250

func _physics_process(delta):
	position += transform.x * speed * delta

func _ready():
	var mob_types = $AnimatedSprite.frames.get_animation_names()
	$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

func _on_Bullet_body_entered(body):
	if body.is_in_group("player"):
		body.queue_free()
	queue_free()

iamrifki | 2022-01-23 14:24

Okay, I figured out the problem, however, I’m still confused about the second post about the enemy following the player, I want the Position2D to rotate to where the player was to target the Mobs.

iamrifki | 2022-01-23 15:05

I think this should be split to a new question, but thanks for the help on this.

iamrifki | 2022-01-24 01:19