Advertisement
Guest User

Rudimentary Random Level Generation in Unity

a guest
Jun 8th, 2014
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. public GameObject blockType; //set this to a prefab in the editor
  2.  
  3. int objectsCreated = 0;
  4.  
  5. //Loop this whole thing until we've created as many blocks as we want
  6. while (objectsCreated < desiredNumberOfBlocks) {   
  7.    
  8.     //Pick a random position within some limits
  9.     float blockX = Random.Range(leftLimit,rightLimit);
  10.     float blockY = Random.Range(levelHalfHeight * -1,levelHalfHeight);
  11.     float blockZ = levelDepth;
  12.     vector3 blockPosition = new Vector3 ( blockX, blockY, blockZ );
  13.    
  14.     //Create the block at that position and remember its name
  15.     GameObject block = Instantiate(blockType, blockPosition, Quaternion.identity) as GameObject;
  16.    
  17.     //Pick a random size within some limits
  18.     float blockWidth =Random.Range(5,maxBlockWidth);
  19.     float blockHeight = Random.Range(5,maxBlockHeight);
  20.     float blockDepth = Random.Range(5,maxBlockDepth)); 
  21.    
  22.     //Make our block that size
  23.     block.transform.localScale = new Vector3(blockWidth, blockHeight, blockDepth);
  24.    
  25.     //Note we've made another block, loop
  26.     objectsCreated = objectsCreated + 1;
  27.    
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement