Infinite runner with dynamic intersections

Godot Version

4.5

Question

Hi,
I am really new to the game development in general and would really appreciate some help.

I am trying to build an infinite runner game with some tweaks. I found some tutorials on how to build the simple infinite runner, where the player basically stands still and you are generating obstacles and move them in the direction of the player, so it seems like infinite.

What I don’t understand is how to build intersections of different types, with obstacles moving from right to left. How do you set up the scene and loop it so every once in a while there is an intersection and it is in loop/infinite.

It’s really hard for me to still understand game mechanics, so I would be really grateful for help!

Thank you,
Nick.

You can build different type of section (straight, left/right turn, intersection, etc.) in a separate godot scene and spawn them randomly.

On top of my head, you can use something like probability for deciding what to spawn.

func _physics_process(delta: float) -> void :

    # -- more code --

    var prob = randi() % 20 # Returns random integer between 0 and 19
    if prob == 19:
        # spawn intersection scene
    elif prob >= 16 or prob <= 18:
        # spawn left turn scene
    elif prob >= 12 or prob <= 15:
        # spawn right turn scene
    else:
        # spawn straight scene

This means intersection scene will have 1/20 probability to be spawned. You might also want to trigger the spawner after like 10 seconds instead of every frame.

For obstacle that moving sideways, while the whole section scene moving toward player at Z-axis, move the obstacle within the scene along the local X-axis. You can have something like Area3D to trigger the obstacle sideways movement when player entered it.