Question by
Mystique5 · Dec 06, 2016 at 02:54 PM ·
instantiateprocedural generationplanetspaceship
Please Explain the Code
Hello,
So recently I have been trying to make a space based game with procedural generation. I found this piece of code which allows me to procedurally generate planets, asteroids and minerals. However, everything that is instantisated is usually located outside of the camera view.
Could you please explain how I could make everything spawn relative to the camera's screen range? I apologize for the extremely long code.
The Item and the SpawnItem are just lists to store the objects to instantiate. So you can ignore them.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlanetGeneration : MonoBehaviour {
public float itemSpacing = 15f;
[Header("Map Size")]
public float width = 100f;
public float height = 100f;
[Header("Sizes of items")]
public float minPlanetSize = 10f;
public float maxPlanetSize = 20f;
public float minAsteroidSize = 5f;
public float maxAsteroidSize = 10f;
public float minMineralSize = 0.5f;
public float maxMineralSize = 5f;
[Header("Prefabs To Spawn")]
public GameObject planetPrefab;
public GameObject asteroidPrefab;
public GameObject goldPrefab;
public GameObject diamondPrefab;
public List<SpawnItem> spawnItemList;
void Start() {
GenerateObjects ();
StartCoroutine(SpawnPrefabsNearby());
}
IEnumerator SpawnPrefabsNearby () {
while (true) {
foreach (SpawnItem item in spawnItemList) {
if (Mathf.Abs(item.pos.y - Warp.Position.y) < 30f) {
//This is to set the distance to which the items will spawn until
if (item.spawnedObject == null && !item.collected) {
item.spawnedObject = (GameObject)Instantiate(item.prefab, item.pos, Quaternion.identity, transform);
item.SetupSpawnedObject();
}
} else {
if (item.spawnedObject != null) {
Destroy(item.spawnedObject);
}
}
}
yield return new WaitForSeconds(2f);
}
}
void GenerateObjects(){
spawnItemList = new List<SpawnItem>();
GenerateItems();
}
void GenerateItems () {
float xAxis = 0f;
while (xAxis < width) {
float yAxis = 0f;
int timeout = 0;
while (yAxis < height) {
float multiplier = ((height - (yAxis - 1f)) / height) * 2f;
float realSpacing;
if (yAxis < height - 15f) {
realSpacing = itemSpacing * multiplier;
} else {
realSpacing = itemSpacing;
}
float xOffset = Random.Range(-itemSpacing / 2f, itemSpacing / 2f);
float yOffset = Random.Range(-realSpacing / 2f, realSpacing / 2f);
Vector3 pos = new Vector3(xAxis + xOffset - width / 2f, Mathf.Clamp(-yAxis - yOffset, -Mathf.Infinity, 1f), 0f);
RegisterSpawnItem(pos, yAxis);
yAxis += realSpacing / 5f; //Controls the Planet density
timeout++;
if (timeout > 10000)
{
Debug.LogError("TIMEOUT");
return;
}
}
xAxis += itemSpacing / 2f;
}
}
void RegisterSpawnItem (Vector3 pos, float depth) {
SpawnItem spawnItem = new SpawnItem ();
spawnItem.pos = pos;
spawnItem.prefab = planetPrefab;
spawnItem.scale = Vector3.one * Random.Range (minPlanetSize, maxPlanetSize);
spawnItemList.Add (spawnItem);
}
}
Thank you
Comment