Obstacles offset does not work

Hello fellows!

I’m learning Godot 3D. I’m working on a infinite runner mobile game. But I’m struggling with iteration of my obstacles.

I’m following this tutorial but when getting to the iteration part there is gaps between each obstacle

I’ve been testing out some alternatives than the one on the video. But nothing have done the work yet.

My question

Is there any way to get the size of my obstacles? Or should I calculate the proper offset for each iteration? If so… I have no idea how to even get close to that :sweat_smile:

My code

Main level script:

extends Node3D

@export var modules: Array[PackedScene]

var amount = 10
var rng = RandomNumberGenerator.new()
var offset = 5

func _ready():
	for n in amount:
		spawn_module(n * offset)

func spawn_module(n):
	rng.randomize()
	var num = rng.randi_range(0, modules.size() - 1)
	var instance = modules[num].instantiate()
	instance.position.z = -n
	add_child(instance)

This script run on each obstacle scene:

extends Node3D

var speed = 5
@onready var level := get_parent()

func _process(delta):
	position.z += speed * delta
	
	if position.z > 5:
		level.spawn_module(position.z + (level.amount * level.offset))
		queue_free()

Thanks for your time!

Props for using KayKit’s models! They’re awesome!

This line of code is your problem:

var offset = 5

Why did you make it 5? It’s clearly too big. Play with the number until it works.

His assets are so good.

I did play with that var but it didn’t work out. The most close try that I have with it still have some gaps between each obstacle on the first iterations but after the 25 for some reason each iteration were overlapping with each other.

Ok, well if you’re moving the background instead of the player, that makes sense.

In that case, you can measure. You’re going to want to get the length of the previous piece, so instead of:

instance.position.z = -n

…do something like:

instance.position.z = previous_instance.position.z + previous_instance.mesh_instance_3d.size.z
1 Like

I tried this out. Gave me the right direction to get to the solution. I needed to make a function to calculate AABB for each asset on the obstacle scene and merge the AABB value.

This was the final value to subtract from the final position.

Thanks for your help!

1 Like