- Home /
How To Endlessly Make Land in a 2D Topdown game
I have been looking for 8 hours to try find a way to endlessly generate chunks and nothing. Plz can someone help me find something that will help me.
Edit: I have a generation script but generating Diagonally doesn't work can anyone help.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Terrain : MonoBehaviour
{
public GameObject Chunk;
public Transform Player;
public int ChunkSize;
public int Chunks;
[SerializeField]List<Vector2> PreChunkedSpots = new List<Vector2>();
List<GameObject> ChunksG = new List<GameObject>();
public float UnloadDistance;
public float ChunkUnder;
void Start()
{
ChunkCreate();
}
private void Update()
{
Vector2 MyPos = transform.position;
Vector2 PlayerPos = Player.position;
if (PlayerPos.x >= MyPos.x + (ChunkSize * ((Chunks / 2) - ChunkUnder)))
{
Vector2 PutPos = new Vector2(MyPos.x + (ChunkSize * Chunks), transform.position.y);
transform.position = PutPos;
}
else if (PlayerPos.x <= MyPos.x - (ChunkSize * ((Chunks / 2) - ChunkUnder)))
{
Vector2 PutPos = new Vector2(MyPos.x - (ChunkSize * Chunks), transform.position.y);
transform.position = PutPos;
}
if (PlayerPos.y >= MyPos.y + (ChunkSize * ((Chunks / 2) - ChunkUnder)))
{
Vector2 PutPos = new Vector2(transform.position.x, MyPos.y + (ChunkSize * Chunks));
transform.position = PutPos;
}
else if (PlayerPos.y <= MyPos.y - (ChunkSize * ((Chunks / 2) - ChunkUnder)))
{
Vector2 PutPos = new Vector2(transform.position.x, MyPos.y - (ChunkSize * Chunks));
transform.position = PutPos;
}
for (int i = 0; i < ChunksG.Count; i++)
{
if (Vector2.Distance(ChunksG[i].transform.position, Player.position) >= UnloadDistance)
{
ChunksG[i].SetActive(false);
}
else
{
ChunksG[i].SetActive(true);
}
}
bool Spawn = true;
for (int i = 0; i < PreChunkedSpots.Count; i++)
{
Vector2 PlayerPoss = transform.position;
if (PlayerPoss == PreChunkedSpots[i])
{
Spawn = false;
break;
}
}
if (Spawn)
{
ChunkCreate();
}
}
public void ChunkCreate()
{
for (int x = 0; x < Chunks; x++)
{
for (int y = 0; y < Chunks; y++)
{
GameObject Inst = Instantiate(Chunk, new Vector2(transform.position.x + (((Chunks / 2) - x) * ChunkSize), transform.position.y + (((Chunks / 2) - y) * ChunkSize)), Quaternion.identity);
Inst.GetComponent<Chunk>().ChunkSize = ChunkSize;
ChunksG.Add(Inst);
}
}
PreChunkedSpots.Add(transform.position);
}
}
Answer by streeetwalker · Sep 20, 2020 at 02:48 PM
There several tutorials that show you how to do this. Google "unity endless repeating background" or some variation thereof.
Answer by BranchOffGameStudio · Sep 20, 2020 at 03:22 PM
There's no one correct way to do this. I've written an asset called Isometric World Builder that I will be releasing in the near future that generates endless topdown procedural landmasses. If you want to start learning some basics of procedural generation I suggest this video series by Sebastian Lague. At the point where he starts using marching squares to generate 3D meshes is the point where you take the data and use it to generate a 2D map. I would suggest you look at the new Unity Tile system as you can easily place different tiles with rules to help give your generated world some polish. Here is a link to a video of the asset I've been working on. Hope this helps!
Your answer
Follow this Question
Related Questions
Rendering only when player is nearby. 1 Answer
How to create an endless track? 1 Answer
Making an endless tunnel 2 Answers
how one would go about using Triangulator in javascript? 0 Answers
Procedural terrain generation ? 0 Answers