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