How can I make enemies spawn and travel to a certain point?

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

Hi Everyone,

I am having trouble with path2Ds. So the enemies spawn at intervals as per the “your first game”. However, what I want is for them to spawn and move towards/go to a stationary character or a set of coordinates from wherever they spawn.

I have tried a bunch of stuff and can’t get it to work. Any ideas?

Code would be appreciated.

(If it makes any difference the player will use a centre of gravity to attract the enemies of their path - would I need to define a new path so that they don’t continue to go back to the hub immediately, but after a certain time?)

func _on_MobTimer_timeout():
$Path2D/PathFollow2D.offset = randi()
var mob = Enemy.instance()
add_child(mob)
var facing = $Hub.position
var direction = facing.rotation
mob.position = $Path2D/PathFollow2D.position
mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
mob.linear_velocity = mob.linear_velocity.rotated(direction)

Thanks.

To be clear, you called randomize() right? What do you mean when you say you can’t get it to work? Have you read this article?

exuin | 2021-03-13 19:07