Endless road generation, but road is only built straight ahead

Godot Version



I created a script that generates a one-way road, but the road only has a forward direction. Does anyone have any ideas on how to add random curvature to the road (sharp turns, etc.)?

P.S. I was inspired by the generation of terrain and roads from the game The Long Drive and decided to repeat similar on Godot Engine.

Here’s the code:

extends Spatial

# Road settings
export var segmentLength = 20.0 # Segment lenght
export var segmentWidth = 8.0 # Segment width
export var roadMaterial = preload("res://road.res") # Road material
export var numSegments = 10 # Startup segments count

var segments = [] # Segments list (array)

func _ready():
# Generate startup segments
for i in range(numSegments):

func generateSegment():
	var segment = MeshInstance.new()
	var mesh = PlaneMesh.new()
	mesh.size = Vector2(segmentLength, segmentWidth)
	segment.mesh = mesh

    segment.transform = Transform(Basis(), Vector3(segments.size() * segmentLength, 0, 0))

    if roadMaterial:
	    segment.material_override = roadMaterial


func _process(delta):
    var player = get_tree().get_root().find_node("actor", true ,false)
    var playerPos = player.global_transform.origin.x / 0.5
    var lastSegmentPos = segments[segments.size() - 1].global_transform.origin.x
    if playerPos > lastSegmentPos - segmentLength * 2.0:

Here’s a screenshot:

I created a random path generator for a 3D game, and it amounted to:

  1. Creating a scene for each type of path object (straight, left turn, right turn, slope up, slope down, slope up turning left, etc.).

  2. Creating a table in each path object that indicate which path objects are allowed to connect with the object, what the probability is for each connection, and the minimum and maximum number of instances that are allowed to connect consecutively before a different type must be selected.

1 Like

Here’s a screenshot of how it looks while being played:

This view consists of multiple randomly generated trench segments going down, straight, and up, along with transition rules not allowing a down trench to immediately follow an up trench, and vice-versa.

Thank you. Well, I reworked the script and added Path and PathFollow nodes. In the script, I made sure that the Curve3D (curve in Path node) points were generated randomly and the PathFollow offset value increased each time one segment was generated. Therefore, the transform of each new segment will be equal to the PathFollow transform corresponding to the PathFollow offset.

I got something like this:

But the turns are too sharp…

So, I created my own little seed system and now this one seed works for both terrain and road. For Curve3D, points are created based on OpenSimplePexNoise. Now it looks smoother, but in some places segments of the road fall under the terrain.

Here’s a screenshot:

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