- Home /
Trying to make things far away deactivate. What can I do to this code to make it happen?
This code executes spawning random terrain in 2 sections. left and right, left being to the left of 0, right vice versa. Im sure there is an easier way to do this, because the code I have isn't working at all. renderL and renderR and so forth is supposed to keep track of the farthest render on each side. is there an easier way to do this? Before attempting to implement this, the chunks spawn when going near them, but now it's really broken due to my attempts.
-Edit-
I took out the parts I was working on because they were really broken, and I added comments
using UnityEngine;
using System;
using System.Collections;
public class TerrainGeneration : MonoBehaviour {
public int width, height;
[Range (0, 100)]
public int fillPercent;
private enum Cubes
{
No,
Yes
};
private Cubes [,] map;
private enum CubeTypes
{
Dirt,
Grass,
Cobblestone,
DirtMoss,
CobblestoneMoss
};
private CubeTypes [,] mapData;
public string seed;
public bool useRandomSeed;
public int smoothness;
public int edgeThickness;
public GameObject [] cubePack_1;
public GameObject player;
public int chunkSize;
public int leftmostChunk, rightmostChunk;
public int hillStrength;
public int hillHeight;
public int hillSample;
public int [] triggerDepthLevels;
public int [] triggerHeightLevels;
public GameObject chunkContainer;
public int renderDistance;
// This constructor checks blocks around the block being plugged in at x, y
int GetNeighbors (int x, int y) {
int neighbors = 0;
for (int x2 = x - 1; x2 < x + 2; x2 ++)
{
for (int y2 = y - 1; y2 < y + 2; y2 ++)
{
if (x2 != x || y2 != y)
{
if (x2 > -1 && x2 < width && y2 > -1 && y2 < height + hillHeight)
{
if (map [x2, y2] == Cubes.Yes)
{
neighbors ++;
}
}
}
}
}
return neighbors;
}
// This constructor checks above the block until going up h times
bool CheckAboveBlock (int x, int y, int h) {
if (height + hillHeight - y > h)
{
for (int y2 = y + 1; y2 < y + h + 1; y2 ++)
{
if (map [x, y2] == Cubes.Yes)
{
return true;
}
}
}
else
{
for (int y2 = y + 1; y2 < height + hillHeight; y2 ++)
{
if (map [x, y2] == Cubes.Yes)
{
return true;
}
}
}
return false;
}
void Start () {
CreateGrid ();
}
void Update () {
// This makes sure all the current rendering is done in one frame
for (int i = 0; i < renderDistance; i ++)
{
// If the player is beyond a certain distance and the whole map hasn't been renderered on that side
if (player.transform.position.x < -leftmostChunk * chunkSize / 4 + chunkSize / 4 * renderDistance)
{
if (leftmostChunk * chunkSize <= width / 2 - chunkSize)
{
CreateCubes (-1);
}
}
if (player.transform.position.x > rightmostChunk * chunkSize / 4 - chunkSize / 4 * renderDistance)
{
if (rightmostChunk * chunkSize <= width / 2 - chunkSize)
{
CreateCubes (1);
}
}
}
}
// Creates arrays to contain block data
void CreateGrid () {
map = new Cubes [width, height + hillHeight];
mapData = new CubeTypes [width, height + hillHeight];
Randomize ();
}
// Based on a seed, this places blocks and empty spaces based on chance of fill percentage and edge thickness
void Randomize () {
if (useRandomSeed == true)
{
seed = Time.time.ToString ();
}
System.Random prng = new System.Random (seed.GetHashCode());
for (int x = 0; x < width; x ++)
{
for (int y = 0; y < height; y ++)
{
if (x < edgeThickness || x > width - edgeThickness - 1 || y < edgeThickness || y > height - edgeThickness - 1)
{
map [x, y] = Cubes.Yes;
}
else if (prng.Next (0, 100) < fillPercent)
{
map [x, y] = Cubes.Yes;
}
}
}
// Hills are pulled out of the ground s amount of times
for (int s = 0; s < hillStrength; s ++)
{
int additiveHeight = prng.Next (hillSample, hillSample + 2);
int newHeight = height + additiveHeight;
BuildHills (newHeight);
}
// Smoothing occurs s2 amount of times to give more public flexibility
for (int s2 = 0; s2 < smoothness; s2 ++)
{
SmoothMap ();
}
DetermineCubes ();
CreateCubes (-1);
CreateCubes (1);
}
// If a block has mroe than 4 neighbors, it becomes a definite cube. If it has exactly 4, it is left alone. Less than 4, it is destroyed.
void SmoothMap () {
for (int x = 0; x < width; x ++)
{
for (int y = 0; y < height + hillHeight; y ++)
{
int neighbors = GetNeighbors (x, y);
if (neighbors > 4)
{
map [x, y] = Cubes.Yes;
}
else if (neighbors < 4)
{
map [x, y] = Cubes.No;
}
}
}
}
// This places some blocks above the ground that are later smoothed out. This is just an early implementation that will be changed later
void BuildHills (int n) {
System.Random prng = new System.Random (seed.GetHashCode ());
for (int x = 0; x < width; x ++)
{
if (prng.Next (1, 4) == 1)
{
for (int y = height - 1; y < height + hillHeight; y ++)
{
if (y < n)
{
map [x, y] = Cubes.Yes;
}
}
}
}
for (int x = 0; x < width; x ++)
{
for (int y = height - 1; y < height + hillHeight; y ++)
{
int neighbors = GetNeighbors (x, y);
if (neighbors == 0)
{
map [x, y] = Cubes.No;
}
}
}
}
// Based on the environment and circumstances of each cube, each space is put into an array of enums that says what each block is
void DetermineCubes () {
for (int x = 0; x < width; x ++)
{
for (int y = 0; y < height + hillHeight; y ++)
{
if (map [x, y] == Cubes.Yes)
{
int neighbors = GetNeighbors (x, y);
if (neighbors < 8)
{
if (y < height + triggerDepthLevels [0])
{
mapData [x, y] = CubeTypes.Cobblestone;
}
}
bool checkAbove = CheckAboveBlock (x, y, 5);
if (checkAbove == false)
{
if (y < height + triggerDepthLevels [1])
{
if (mapData [x, y] == CubeTypes.Dirt)
{
mapData [x, y] = CubeTypes.DirtMoss;
}
else if (mapData [x, y] == CubeTypes.Cobblestone)
{
mapData [x, y] = CubeTypes.CobblestoneMoss;
}
}
else if (mapData [x, y] == CubeTypes.Dirt)
{
mapData [x, y] = CubeTypes.Grass;
}
}
}
}
}
}
// This actually instantiates the cubes. Some of these fomulas are complicated due to the player starting in the middle of the map.
void CreateCubes (int direction) {
if (direction == -1)
{
GameObject currentChunk = Instantiate (chunkContainer, Vector3.zero, Quaternion.identity) as GameObject;
currentChunk.gameObject.name = "Chunk Container " + "L" + (leftmostChunk + 1);
for (int x = 0; x < chunkSize; x ++)
{
for (int y = 0; y < height + hillHeight; y ++)
{
if (map != null && mapData != null)
{
if (map [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == Cubes.Yes)
{
Vector2 pos = new Vector3 ((float) x / 4 - chunkSize / 4 - leftmostChunk * chunkSize / 4 + 0.125f, (float) y / 4 - height / 2 + 0.125f);
if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Dirt)
{
GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Grass)
{
GameObject currentCube = Instantiate (cubePack_1 [1], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Cobblestone)
{
GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.DirtMoss)
{
GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.CobblestoneMoss)
{
GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
}
}
}
}
leftmostChunk ++;
}
else if (direction == 1)
{
GameObject currentChunk = Instantiate (chunkContainer, Vector3.zero, Quaternion.identity) as GameObject;
currentChunk.gameObject.name = "Chunk Container " + "R" + (rightmostChunk + 1);
for (int x = 0; x < chunkSize; x ++)
{
for (int y = 0; y < height + hillHeight; y ++)
{
if (map != null && mapData != null)
{
if (map [width / 2 + rightmostChunk * chunkSize + x, y] == Cubes.Yes)
{
Vector2 pos = new Vector3 ((float) x / 4 + rightmostChunk * chunkSize / 4 + 0.125f, (float) y / 4 - height / 2 + 0.125f);
if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Dirt)
{
GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Grass)
{
GameObject currentCube = Instantiate (cubePack_1 [1], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Cobblestone)
{
GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.DirtMoss)
{
GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.CobblestoneMoss)
{
GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
currentCube.transform.parent = currentChunk.transform;
}
}
}
}
}
rightmostChunk ++;
}
}
}
Please do some prepwork and testing on your own and then post a much smaller chunk of code, which exactly states your problem. Nobody is willing to read and think about your whole class. Figure out what exactly is not working and make a new script that only tests this one thing. Usually this way you find the problem yourself, but then its also much easier to ask others. ;)
I tried so much, like for hours. I cant seem to find whats wrong.
All that is important is update and the last 3 voids at the bottom.
Some comments in your code would be apreciated too ^^
@$$anonymous$$dRWaylander. Here, I deleted the new parts I was working on because I found it useless to have broken code in there, and I put in comments