No collision detected in _onready for Area3D

Godot Version

4.2.1-stable_win64

Question

During my _onready I’m trying to ensure all my coins are not overlapping to begin with. I’m not sure why this collision detection is not detecting a coin that is intersecting another one. Does this have something to do with _onready? Please see the .gif. Here’s my code:

#the -6.9 and 6.9 is the boundaries of the box the coins are meant to be restricted into.
func _ready():
	add_to_group('coins')
	position.y = 0.73
	position.z = randf_range(-6.9, 6.9) 
	position.x = randf_range(-6.9, 6.9) 
	while area3d.has_overlapping_areas():
		print('overlap')
		position.x = randf_range(-6.9, 6.9) 
		position.z = randf_range(-6.9, 6.9)

coins

Thanks!

I would assume that the collision checks occur on the next physics frame after spawning the objects, so spawning them in bulk and them checking if they overlap wont produce results.

Yes, I did some tests and adding await did fix the issue and made coins realise they were overlapping… however, it is not a good solution to this problem.

Easiest suggestion would be to do a checks manually:

for coin in get_tree().get_nodes_in_group('coins'):
	if ...

but that one is also kinda hefty on the performance…
I feel like there is an easier solution of you have an additional CoinSpawner that spawns coins and has an array of all of them.

1 Like

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