Created
August 16, 2018 22:46
-
-
Save Drenerdo/8f690b3b0e70889f7f8395664092b79a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Random = UnityEngine.Random; | |
public class ObjectSpawner : MonoBehaviour | |
{ | |
public GameObject block1; | |
public int worldWidth = 10; | |
public int worldHeight = 10; | |
private Vector3[] cubePoints; | |
private List<GameObject> activeCubes; | |
private List<Transform> startPositions; | |
public float spawnSpeed = 0; | |
private float smallCubeVol; | |
void Start() | |
{ | |
Debug.Log("Started"); | |
activeCubes = new List<GameObject>(); | |
startPositions = new List<Transform>(); | |
GetTransforms((int)Math.Pow(30,3)); | |
// StartCoroutine(SpawnCubes()); | |
SpawnCubes2(); | |
} | |
void GetTransforms(int cubeAmount) | |
{ | |
GameObject BigCube = GameObject.FindGameObjectWithTag("Cubes"); | |
Debug.Log(BigCube.transform.localScale); | |
smallCubeVol = Mathf.Pow(BigCube.transform.lossyScale.x / Mathf.Pow(cubeAmount, 1f/3f),3); | |
float singleVector = smallCubeVol / Mathf.Pow(cubeAmount, 1f/3f); | |
Debug.Log(singleVector); | |
Vector3 sp = new Vector3(BigCube.transform.position.x + singleVector, BigCube.transform.position.y + singleVector, BigCube.transform.position.z + singleVector); | |
// GameObject[] prefabCubes = GameObject.FindGameObjectsWithTag("Cubes"); | |
cubePoints = new Vector3[cubeAmount]; | |
int xMax = (int)(Mathf.Pow(cubeAmount, 1f /3f)); | |
// float xMax = (Mathf.Sqrt(cubeAmount)); | |
// Debug.Log(xMax); | |
for (int i = 0; i < cubeAmount; i++){ | |
// Debug.Log("Transform found: " + cube.transform.position); | |
float xIdx = (int)(i % xMax) * (singleVector); | |
float yIdx = (int)((i % (xMax * xMax) ) / xMax)* (singleVector); | |
float zIdx = (int)(i / (xMax * xMax)) * (singleVector); | |
Debug.Log(yIdx+","+zIdx); | |
Vector3 v = new Vector3(sp.x + xIdx, sp.y + yIdx, sp.z + zIdx); | |
cubePoints[i] = v; | |
// Debug.Log(v+" : "+i); | |
// cube.SetActive(false); | |
// index++; | |
} | |
} | |
// IEnumerator SpawnCubes() | |
// { | |
// for(int i = 0; i < cubePoints.Length; i++) | |
// { | |
// yield return null; | |
// float randX = Random.Range(-5f, 5f); | |
// float randZ = Random.Range(-5f, 5f); | |
// float randY = Random.Range(-1f, 0f); | |
// Vector3 randVec = new Vector3(randX, randY, randZ); | |
// GameObject block = Instantiate(block1, Vector3.zero, block1.transform.rotation) as GameObject; | |
// float size = .2f; | |
// block.transform.localScale = new Vector3(.25f, .25f, .25f); | |
// //Rigidbody rb = block.GetComponent<Rigidbody>(); | |
// //block.transform.parent = transform; | |
// block.transform.localPosition = randVec; | |
// startPositions.Add(block.transform); | |
// activeCubes.Add(block); | |
// } | |
// StartCoroutine(MoveIntoCube()); | |
// } | |
public void SpawnCubes2() | |
{ | |
for (int i = 0; i < cubePoints.Length; i++) | |
{ | |
float randX = Random.Range(-5f, 5f); | |
float randZ = Random.Range(-5f, 5f); | |
float randY = Random.Range(0f, 0f); | |
Vector3 randVec = new Vector3(randX, randY, randZ); | |
GameObject block = Instantiate(block1, Vector3.zero, block1.transform.rotation) as GameObject; | |
// float size = Mathf.Pow(smallCubeVol, 1f / 3f); | |
float size = .3f; | |
block.transform.localScale = new Vector3(size,size,size); | |
block.GetComponent<MeshRenderer>().enabled = true; | |
//Rigidbody rb = block.GetComponent<Rigidbody>(); | |
//block.transform.parent = transform; | |
block.transform.localPosition = randVec; | |
startPositions.Add(block.transform); | |
activeCubes.Add(block); | |
} | |
StartCoroutine(MoveIntoCube()); | |
} | |
IEnumerator MoveIntoCube() | |
{ | |
float timeSinceStart = 0f; | |
float timeToComplete = 1f; | |
while (timeSinceStart < timeToComplete) | |
{ | |
float percent = timeSinceStart / timeToComplete; | |
for (int i = 0; i < activeCubes.Count; i++) | |
{ | |
// Debug.Log("Attempting to move"); | |
Vector3 startPos = startPositions[i].position; | |
Vector3 targetPos = cubePoints[i]; | |
GameObject cube = activeCubes[i]; | |
//Rigidbody rb = cube.GetComponent<Rigidbody>(); | |
//rb.isKinematic = false; | |
cube.transform.position = Vector3.Lerp(startPos, targetPos, percent); | |
} | |
timeSinceStart += Time.deltaTime; | |
yield return null; | |
} | |
} | |
//IEnumerator CreateWorld() | |
//{ | |
// for (int x = 0; x < worldWidth; x++) | |
// { | |
// yield return new WaitForSeconds(spawnSpeed); | |
// for (int z = 0; z < worldHeight; z++) | |
// { | |
// yield return new WaitForSeconds(spawnSpeed); | |
// GameObject block = Instantiate(block1, Vector3.zero, block1.transform.rotation) as GameObject; | |
// //block.transform.parent = transform; | |
// block.transform.position = new Vector3(x, 0, z); | |
// } | |
// } | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment