Make objects randomly spawning and falling to the player

Godot Version

Godot Version 4.2.2

Question

Hello everyone, beginner here! Im currently making a game where a character collects falling coins that spawn randomly on top of him.

The coin has Area2D and CollisionShape2D as its nodes (it doesnt have any script yet) if that helps.

Help is very much appreciated, thank you!


Attact the script to the coin:

func _physical_process(delta):
   position.y += 700 * delta

Now this script to the game node:

func _ready():
   var timer = Timer.new()
   timer.wait_time = 1.7
   timer.auto_start = true
   add_child(timer)
   timer.timeout.connect(_on_timer_timeout)

func _on_timer_timeout():
   randomize()
   var coin = preload("path/to/your/coin.tscn").instantiate()
   coin.position.y = -25 #Replace it by the top position of screen
   coin.position.x = randf_range(-40, 40) #replace by start and end of screensize
   add_child(coin)

When you paste this code in the script, press CTRL + S.

If you need far help from me as a friend then contact me on discord, king_game_developer

Thank you very much

1 Like

wow, this is awesome! I was just thinking about trying to make a “catch falling object” type game in Godot (noob) and this is a great start of code to use and play around with! Do you by any chance have an updated project of this ‘game’ you were working on to share?

And, if you have/want to randomly spawn multiple falling objects a various places on a hoizontal line (for the character to move and try to catch/collide with) is that just a matter of copy paste of the “coins” node in your screenshot above?