Godot Version
4.3
Question
`

im making one an infinate run for mobile and my main problem is with speed_item and obsticale_item instantiate. I wanted them to be inside the road and one of my first thoughts was to add a area2d (yellow) and code the items to be instantiate inside it. here is the instantiate code:
func generate_items(player):
items_generated = true
# Get the collision shape of the ItemGenerator
var collision_shape = $"Item-generator/CollisionShape2D"
if not collision_shape:
push_error("CollisionShape2D not found in ItemGenerator!")
return
var shape = collision_shape.shape
if not shape is RectangleShape2D:
push_error("ItemGenerator shape is not a RectangleShape2D!")
return
var rect = shape.get_rect()
var area_global_pos = $"Item-generator".global_position
var area_start_x = area_global_pos.x + rect.position.x
var area_end_x = area_start_x + rect.size.x
# Get the lane positions
var lane1_y
var lane2_y
if road_type == "easy":
# Easy road has only 2 ColorRects
lane1_y = $ColorRect.position.y
lane2_y = $ColorRect2.position.y
else:
# Medium road has 5 ColorRects
lane1_y = $ColorRect2.position.y
lane2_y = $ColorRect4.position.y
var player_lane = player.lane
var target_lane_y = lane1_y if player_lane == 0 else lane2_y
var should_generate_obstacles = player.current_speed > 10
var num_items = randi() % 3 + 2
for i in range(num_items):
# Random X position within the ItemGenerator area
var x_pos = randf_range(area_start_x + 50, area_end_x - 50)
# Adjust probabilities based on player speed
if should_generate_obstacles:
# Normal mode: 30% chance for speed items, 70% for obstacles
if randf() < 0.3:
var item = SPEED_ITEM_SCENE.instantiate()
item.position = Vector2(x_pos, target_lane_y)
get_parent().add_child(item) # Add to the map generator node
else:
var obstacle = OBSTACLE_SCENE.instantiate()
obstacle.position = Vector2(x_pos, target_lane_y)
get_parent().add_child(obstacle) # Add to the map generator node
else:
# Low speed mode: 90% chance for speed items, no obstacles
if randf() < 0.9:
var item = SPEED_ITEM_SCENE.instantiate()
item.position = Vector2(x_pos, target_lane_y)
get_parent().add_child(item) # Add to the map generator node
after a while i have no idea what to do anymore. also the items should have generated on the next road only but they also get generated on the previouse road.
`