Can anyone help me with a method i could used to create space or distance between spawns

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

I have a code like this but im a facing a problem, my coins are overlapping each other

func _ready():
randomize()
for i in range(3):
var Coins1 = preload(“res://Assets/Coins/Coin_Collection_1.tscn”).instance()
add_child(Coins1)
Coins1.global_position.x = rand_range(-3500,6000)
Coins1.global_position.x = rand_range(-1000,650)
for i in range(1):
var Coins2 = preload(“res://Assets/Coins/Coin_Collection_2.tscn”).instance()
add_child(Coins2)
Coins2.global_position.x = rand_range(-3500,6000)
Coins2.global_position.x = rand_range(-1000,650)
for i in range(4):
var Coins3 = preload(“res://Assets/Coins/Coin_Collection_3.tscn”).instance()
add_child(Coins3)
Coins3.global_position.x = rand_range(-3500,6000)
Coins3.global_position.x = rand_range(-1000,650)
for i in range(2):
var Coins4 = preload(“res://Assets/Coins/Coin_Collection_4.tscn”).instance()
add_child(Coins4)
Coins4.global_position.x = rand_range(-3500,6000)
Coins4.global_position.x = rand_range(-1000,650)

:bust_in_silhouette: Reply From: BakouKai

Hi,

I am no expert but there may be a few problems with your code (cannot see the indent).

You are always changing position.x never y (might be normal for a platform).
You randomize once in the beginning. that could be the problem.
I would try to make a Global method for randomization :

func roll_dice(start, finish):
	var rng = RandomNumberGenerator.new()
	rng.randomize()
	return rng.randi_range(start, finish)

And call it everytime you need a random number. This way you should not have the same number:

Coins1.globalposition.x = Global.roll_dice(-3500,6000)
Coins1.globalposition.x = Global.roll_dice(-1000,650)

Yet, you could also change the range according to previous coinspawn (to avoid having them too close, or too random), maybe by having a check like :
if a coin position is closer than 200, reroll dices.

Hope this helps.