How to add multiple instance to PathFollow2D without overlapping?

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

Hello, i am trying to add instance to pathfollow2d by a button. I can add first instance without any problem. The instance follows path. But when i click button that creates instance to create and add new instance , it coming over instance that created right before.

I expect to see instances added and moving independently but it comes right over it. How can i create and add to path2d without overlapping?

My Button Code:

	var scene = preload("res://enemies/enemy.tscn")
var instance = scene.instance()
var path2dObj= get_node("Path2D/PathFollow2D")
path2dObj.add_child(instance)

My PathFollow2D Code:

func _process(delta):
set_offset(get_offset() + speed * delta)

I solved it myself. For anyone looking for answer: Every instance needs it’s own PathFollow2D. This PathFollow2D item needs to added as child to Path2D.

So, first you need to create enemy instance, then you should create instance PathFollow2D. Then you need to add enemy instance to PathFollow2D instance. Final step is adding PathFollow2D instance to Path2D

var enemy= preload("res://enemies/enemy.tscn")
var path = preload("res://levels/Path.tscn")
var pathInstance = path.instance()
var enemyInstance = enemy.instance()
pathInstance.add_child(enemyInstance)
get_node("Path2D").add_child(pathInstance)

emreyvz | 2022-03-26 18:24

:bust_in_silhouette: Reply From: emreyvz

I solved it myself. For anyone looking for answer: Every instance needs it’s own PathFollow2D. This PathFollow2D item needs to added as child to Path2D.

So, first you need to create enemy instance, then you should create instance PathFollow2D. Then you need to add enemy instance to PathFollow2D instance. Final step is adding PathFollow2D instance to Path2D

var enemy= preload("res://enemies/enemy.tscn")
var path = preload("res://levels/Path.tscn")
var pathInstance = path.instance()
var enemyInstance = enemy.instance()
pathInstance.add_child(enemyInstance)
get_node("Path2D").add_child(pathInstance)

.
Don’t forget to draw path of PathFollow2D on Editor