So I am trying to make a simple 2D sand box game, and I’m trying to generate the background tiles behind the foreground ones, but when I generate my world the background tiles are no where to be seen.
Here’s my code to Generate a chunk:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldGeneration : MonoBehaviour {
public GameObject dirtTile;
public GameObject grassTile;
public GameObject stoneTile;
public GameObject dirtBackground;
public GameObject stoneBackground;
public int width;
public float heightMultiplier;
public int heightAddition;
public float smoothness;
[HideInInspector]
public float seed;
void Start ()
{
Generate();
seed = Random.Range(-10000f, 10000f);
}
public void Generate()
{
for (int i = 0; i < width; i++)
{
int h = Mathf.RoundToInt(Mathf.PerlinNoise(seed, (i + transform.position.x) / smoothness) * heightMultiplier) + heightAddition;
for (int j = 0; j < h; j++)
{
GameObject selectedTile;
GameObject backgroundTile;
if(j < h - 4)
{
backgroundTile = stoneBackground;
selectedTile = stoneTile;
} else if (j < h - 1)
{
selectedTile = dirtTile;
backgroundTile = dirtBackground;
}
else
{
backgroundTile = dirtBackground;
selectedTile = grassTile;
}
GameObject newTile = Instantiate(selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
GameObject newBackgroundTile = Instantiate(backgroundTile, Vector3.zero, Quaternion.identity) as GameObject;
newTile.transform.parent = this.gameObject.transform;
newTile.transform.localPosition = new Vector3(i, j);
}
}
}
}