Random projectiles

Im making my first 2d game. Its a horizontal traversal game where projectiles will randomly come from the sides and you have to survive(a lot like the flight sections on cupead). Im finished with the basic player movement but I dont even know where to start with the projectiles. Any help would be appreciated. It would also be nice if there was a way to time the projectiles with music though thats probably to complicated.

Recapping tho 2D tutorial I would use an Area2D scene which has the Nodes for the projectiles. Like Area2D, CollisionShape2D, Sprite2D. This one gets preloaded with @onready var projectile. Next use a Path2D with a rect around your scene. The Script will use rand() on the path to spawn the bullets. Some calls to look_at(player) will get the rotation of the projectile right and move it towards it with velocity = hit_it_like_you_mean_it.

Check out

for a better representation. Try that and we get the rest covered.

1 Like

Is there a way to make the obstacles in my game spawn relative to my position on the x-axis? Path2d seems geared more towards tower defence.

Sure thing there is.
After you instantiated a scene, added it to the tree, and set its position, you can then set the X component of its position to be the same as the node’s you instantiated it from.

new_node.global_position.x = self.global_position.x

heres my code.

extends Node

@export var bat_scene: PackedScene
@export var spike_scene: PackedScene

func _ready() -> void:
	spawn(bat_scene)
	spawn(spike_scene)

func spawn(type):
	print("just checking")
	var bat1 = $bat_point1
	var bat2 = $bat_point2
	var bat3 = $bat_point3
	var bat4 = $bat_point4
	var bat5 = $bat_point5
	var bat6 = $bat_point6
	var spike1 = $spike_point1
	if type == bat_scene:
		var spawn1 = bat_scene.instantiate()
		spawn1.global_position = bat1.global_position
		print("hello bat")
		var spawn2 = bat_scene.instantiate()
		spawn2.global_position = bat2.global_position
		print("hello bat2")
		var spawn3 = bat_scene.instantiate()
		spawn3.global_position = bat3.global_position
		print("hello bat3")
		var spawn4 = bat_scene.instantiate()
		spawn4.global_position = bat4.global_position
		print("hello bat4")
		var spawn5 = bat_scene.instantiate()
		spawn5.global_position = bat5.global_position
		print("hello bat5")
		var spawn6 = bat_scene.instantiate()
		spawn6.global_position = bat6.global_position
		print("hello bat6")
	elif type == spike_scene:
		var spawn1 = spike_scene.instantiate()
		spawn1.global_position = spike1.global_position
		print("hello spike")

What do you mean it doesn’t work? Any error? Be specific please.
Please also paste your code as preformatted text with ```

2 Likes

I added some print checks to the if statement and the if statement is returning those prints on play. I also fixed the syntax in the spike part(i edited the previous reply to match these changes). No issues are showing up in the debugger. The only issue is that they arent visible on play(this is true even without a backround). Also the point of the code is to spawn the bat_scene or spike_scene on each marker2d. It isnt supposed to do anything else yet. I also dont have any code on the enemy scenes yet.

I figured it out

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