Preview:
extends Node3D

var road_connections = [preload("res://city builder/road_forward/road_forward.obj"),
						preload("res://city builder/road_turn_right/road_turn_right.obj"),
						preload("res://city builder/road_t_connection/road_t_connection.obj"),
						preload("res://city builder/road_cross_section/road_cross_section.obj") ]

func _ready():
	update_mesh()

func update_mesh():
	for i in $rays.get_children():
		i.force_raycast_update()
	
	if $rays/ray.get_collider() != null || $rays/ray2.get_collider() != null || $rays/ray3.get_collider() != null || $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[0]
		rotation_degrees = Vector3(0, check_if_front(), 0)
	
	check_for_turns()
	
	check_for_t_connection()
	
	if $rays/ray.get_collider() != null && $rays/ray2.get_collider() != null && $rays/ray3.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[3]

func check_for_turns():
	if $rays/ray.get_collider() != null && $rays/ray3.get_collider() != null:
		$mesh.mesh = road_connections[1]
		rotation_degrees = Vector3(0,0,0)
	elif $rays/ray2.get_collider() != null && $rays/ray3.get_collider() != null:
		$mesh.mesh = road_connections[1]
		rotation_degrees = Vector3(0,90,0)
	elif $rays/ray.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[1]
		rotation_degrees = Vector3(0,-90,0)
	elif $rays/ray2.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[1]
		rotation_degrees = Vector3(0,-180,0)

func check_for_t_connection():
	if $rays/ray2.get_collider() != null && $rays/ray3.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[2]
		rotation_degrees = Vector3(0, 180, 0)
		
	elif $rays/ray.get_collider() != null && $rays/ray3.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[2]
		rotation_degrees = Vector3(0, 0, 0)
		
	elif $rays/ray.get_collider() != null && $rays/ray2.get_collider() != null && $rays/ray4.get_collider() != null:
		$mesh.mesh = road_connections[2]
		rotation_degrees = Vector3(0, -90, 0)
	
	elif $rays/ray.get_collider() != null && $rays/ray2.get_collider() != null && $rays/ray3.get_collider() != null:
		$mesh.mesh = road_connections[2]
		rotation_degrees = Vector3(0, 90, 0)

func check_if_front():
	if $rays/ray.get_collider() != null:
		return -90
	if $rays/ray2.get_collider() != null:
		return 90
	if $rays/ray.get_collider() != null:
		return 0
	if $rays/ray2.get_collider() != null:
		return 0
	else:
		return 0

func return_facing_dir(obj):
	$rays/ray.force_raycast_update()
	
	if $rays/ray.get_collider() != obj:
		return false
	return true
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter