Hello guys,
I need help in adding 2D grass texture on top of my procedurally generated 2D dirt in my side scroller game. The terrain is similar to Hill climb racing, except I cannot figure out how to add grass on top of it.
The video I used to create this 2D generated terrain is: https://www.youtube.com/watch?v=QLZa1mjW-YU
extends Node2D
@export var num_hills = 2.0
@export var slice = 10
@export var hill_range = 700
var screensize
var terrain = Array()
var texture = preload("res://assets/Images/Terrain/DirtBG.png")
func _ready():
randomize()
screensize = get_viewport().get_visible_rect().size
terrain = Array()
var start_y = -100
#screensize.y * 3/4 + (-hill_range + randi() % hill_range*2)
terrain.append(Vector2(0, 0))
add_hills()
func _process(delta):
if terrain[-1].x < $Player.position.x + screensize.x / 0.1:
add_hills()
func add_hills():
var hill_width = screensize.x / (num_hills/10)
var hill_slices = hill_width / slice
var start = terrain[-1]
var poly = PackedVector2Array()
for i in range(num_hills):
var height = randi() % hill_range
start.y -= height
for j in range(0, hill_slices):
var hill_point = Vector2()
hill_point.x = start.x + j * slice + hill_width * i
hill_point.y = start.y + height * cos(2 * PI / hill_slices * j)
#$Line2D.add_point(hill_point)
terrain.append(hill_point)
poly.append(hill_point)
start.y += height
var shape = CollisionPolygon2D.new()
var ground = Polygon2D.new()
$StaticBody2D.add_child(shape)
poly.append(Vector2(terrain[-1].x, screensize.y))
poly.append(Vector2(start.x, screensize.y))
shape.polygon = poly
ground.polygon = poly
ground.texture = texture
add_child(ground)
This is currently what my terrain looks like:
Can anyone help me please?
Thank you for your time