I am trying to create a game that has a purely random floor generation to it. I tried pure random for lower system requirements, but I am having trouble with it currently, and think procedural generation might be the better way. I have just 4 pieces made in Blender and imported into Unity. They are a flat square, one corner up(outside corner piece), two corners up (side piece) and 3 corners up(inside corner piece). And for all this, generating it on the fly instantly (literally as you walk) would probably take a lot more performance than procedural generation (I think) and produce uglier maps since it is just random. I don’t know a lot about procedural generation, besides that it will always produce the same thing from the same seed, and performance may not be as great as I hope. But my game only allows floors to exist within a 120u range currently, and each floor is 30x30u(so fairly big pieces). I was hoping I could get some guidance on how I could achieve what I wish to have in my game. I provided my BuildManager script (the script which controls all the generation/destruction of floor pieces).
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BuildManager : MonoBehaviour {
public List<GameObject> floorsMade = new List<GameObject>();//ALL FLOORS MADE
public List<GameObject> Floors = new List<GameObject>();//FLOORS USABLE
public int floorSize;//SIZE OF FLOOR
public GameObject player;//THE PLAYER,RIGIDBODY
List<Vector3> floorSpotsAroundPlayer = new List<Vector3>();//LIST OF ALL SPOTS AROUND AND INCLUDING currentFloor
List<bool> floorBool = new List<bool> ();//BOOL FOR DETERMINING EMPTY SPOTS AROUND currentFloor
Vector3 currentFloor;//GRABBED FROM GameManager
Vector3 previousFloor;//USED FOR DETERMINING IF THE PLAYER HAS MOVED TO ANOTHER FLOOR
bool startGame;//USED TO INSTANTIATE FIRST FLOOR
void Start () {
setReferences ();
}
void Update(){
StartGame ();
MainFloorManager ();
}
//SET BASE VARIABLES-CLEANS UP SCRIPT
public void setReferences(){
startGame = false;//LETS START GAME START
player = GameObject.Find ("Player");//PLAYER POSITION
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, 0));//LIST OF ALL FLOOR POSITIONS INCLUDING currentFloor
floorSpotsAroundPlayer.Add (new Vector3 (-floorSize, -50,floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (floorSize, -50, floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (floorSize, -50, 0));
floorSpotsAroundPlayer.Add (new Vector3 (floorSize, -50, -floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, -floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (-floorSize,-50, -floorSize));
floorSpotsAroundPlayer.Add (new Vector3 (-floorSize, -50, 0));
ClearFloorBool ();//CLEARS OUT floorBool TO ALL FALSE TILL NEXT CHECK
}
//CREATES AND ADDS FIRST FLOOR TO GAME/floorsMade LIST
public void StartGame(){
if (!startGame) {
startGame = true;
GameObject temp = Instantiate (Floors[0], floorSpotsAroundPlayer [0], Quaternion.identity) as GameObject;
temp.transform.parent = gameObject.transform;
floorsMade.Add (temp);
previousFloor = GameObject.Find ("GameManager").GetComponent<GameManager> ().currentFloor;
}
}
//DOES ALL THE WORK TO INSTANTIATE FLOORS CORRECTLY
public void MainFloorManager(){
currentFloor = GameObject.Find ("GameManager").GetComponent<GameManager> ().currentFloor;
if (currentFloor != previousFloor) {
previousFloor = currentFloor;
CreateBoolList();
//INSTANTIATE FLOORS
for (int i = 0; i < 8; i++) {
if (!floorBool [i]) {
int randNum = Random.Range (0, 4);
Vector3 tempVect = new Vector3 (currentFloor.x + floorSpotsAroundPlayer [i + 1].x, floorSpotsAroundPlayer [i + 1].y, currentFloor.z + floorSpotsAroundPlayer [i + 1].z);
GameObject temp = Instantiate (Floors[randNum], tempVect, Quaternion.identity) as GameObject;
temp.transform.parent = gameObject.transform;
floorsMade.Add (temp);
}
}
ClearFloorBool ();
//DELETE FLOORS
for (int i = floorsMade.Count - 1; i >= 0; i--) {//DELETE FLOORS THAT ARE TOO FAR AWAY. BETTER EFFECT AND TOO FAR AWAY IS USLESS
if (Vector3.Distance (floorsMade[i].transform.position,currentFloor) > floorSize * 4) {
Destroy (floorsMade[i]);
floorsMade.RemoveAt (i);
}
}
}
}
//RESET floorBool LIST FOR CHECKING LATER
public void ClearFloorBool(){
floorBool = new List<bool> ();
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
}
//DRAWS LIMITS AROUND PLAYER FOR DESTROYING FLOORS
void OnDrawGizmosSelected() {
Vector3 playerPos = player.transform.position;
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(playerPos, floorSize * 4 + 15);
}
//CREATES BOOLEAN LIST FOR floorBool
public void CreateBoolList(){
foreach (GameObject floor in floorsMade) {
for (int i = 1; i < 9; i++) {
Vector3 tempvect = new Vector3 (floor.transform.position.x - currentFloor.x,-50,floor.transform.position.z - currentFloor.z);
if (tempvect.x == floorSpotsAroundPlayer[i].x && tempvect.z == floorSpotsAroundPlayer[i].z) {
floorBool [i - 1] = true;//MINUS ONE BECAUSE SKIPPED ONE IN THE 'FOR' STATEMENT, WHICH IS EQUAL TO CURRENT FLOOR
}
}
}
}
}