Moving a marker2d

Im using a marker2d to spawn obstacles for the player to get through and I want the marker 2ds to move with the player rather than having to add a bunch of markers but I dont know how. This is what ive done so far:

extends Marker2D

var speed = 100
var target_pos
var player_pos
@onready var player = get_parent().get_node("New")

func _physics_process(delta: float) -> void:
	player_pos  = player.position.x
	target_pos = player_pos - position


Also I want it to only follow the player on the x axis.

Could you child the Marker2D to the player?

1 Like

Unfortunately this doesnt work. making the markers children of the player node messes with the code I use to spawn them in. Specifically when I try to call on the markers as @onready variables in the scene root code. I can show you the code for spawning them in if you want.

Please do share the code. Which “them” are you referring to here?

Are you spawning the markers in or the obstacles? If it’s the latter then I’m guessing you need to use the marker’s global position.

The markers are already in the scene so im not spawning the markers. Im using the markers to spawn packedscenes(obstacle scenes). I also dont think you can use move_and_slide() with a marker2d, which may be another problem. “them” refers to the obstacles and heres the code for spawning them in:

extends Node

@export var bat_scene: PackedScene
@export var spike_scene: PackedScene
@onready var wave_timer: Timer = $waves
@onready var bat1 = $bat_point1
@onready var bat2 = $bat_point2
@onready var bat3 = $bat_point3
@onready var bat4 = $bat_point4
@onready var bat5 = $bat_point5
@onready var bat6 = $bat_point6
@onready var spike1 = $spike_point1
var waves = 10


func _ready() -> void:
	var start = 0
	spawn(bat_scene)
	spawn(spike_scene)
	while start < waves:
		start += 1
		print(start)
		wave_timer.start()
		await wave_timer.timeout
		print("success")
		spawn(bat_scene)
		spawn(spike_scene)


func spawn(type):
	print("just checking")
	if type == bat_scene:
		spawn_bats()
	elif type == spike_scene:
		spawn_spikes()
		
func spawn_bats():
	var spawn1 = bat_scene.instantiate()
	add_child(spawn1)
	spawn1.global_position = bat1.global_position
	print("hello bat")
	var spawn2 = bat_scene.instantiate()
	add_child(spawn2)
	spawn2.global_position = bat2.global_position
	print("hello bat2")
	var spawn3 = bat_scene.instantiate()
	add_child(spawn3)
	spawn3.global_position = bat3.global_position
	print("hello bat3")
	var spawn4 = bat_scene.instantiate()
	add_child(spawn4)
	spawn4.global_position = bat4.global_position
	print("hello bat4")
	var spawn5 = bat_scene.instantiate()
	add_child(spawn5)
	spawn5.global_position = bat5.global_position
	print("hello bat5")
	var spawn6 = bat_scene.instantiate()
	add_child(spawn6)
	spawn6.global_position = bat6.global_position
	print("hello bat6")

func spawn_spikes():
	var spawn1 = spike_scene.instantiate()
	add_child(spawn1)
	spawn1.global_position = spike1.global_position
	print("hello bat6")

Ok so the markers are bat1-6 and spike1? If you make them a child of your player then your path would be presumably $New/bat-point1 through 6 and $New/spike_point1. You do not need to use move_and_slide() with the marker2D, as a child node inherits it’s parent’s transform so the marker will automatically move with the player, no script needed.

2 Likes

That worked perfectly. thanks for the help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.