Advertisement
Guest User

ForUntyAnswers

a guest
Mar 9th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. RIght to explain the code, first start a new scene and do this.
  2.  
  3.  1. Create a new scene
  4.  2. Add the script to a game object with a visible object like a cube or sphere
  5.  3. populate the prefabs array/List on the object with some cubes of different variations(i tested with cubs with different materials)
  6.  4. set a transform to an object and stick it close to the player.
  7.  5. run
  8.  
  9. I have tried to document the script and make it as self documenting as possible, if there are any questions let me know.
  10.  
  11.  
  12.  
  13.     using UnityEngine;
  14.     using System.Collections;
  15.     using System.Collections.Generic;
  16.    
  17.     public class LevelBlockSpawner : MonoBehaviour {
  18.        
  19.         public List<GameObject> prefabs = new List<GameObject>();           // This contains the different type of blocks
  20.         private List<GameObject> blocksInLevel = new List<GameObject>();    // This contains a reference to current configuration of the block in game 
  21.        
  22.        
  23.         private GameObject BlockStorage;                                    // This GameObject is used to organise all the instantiated cubes  
  24.         private Vector3 lastBlockPosition = new Vector3(0,0,0);             //
  25.            
  26.         // THIS IS FOR DEMONSTRATION YOU MAY REMOVE THIS
  27.         // -------------------------------------------------------------------------------------------------------------------------------------------
  28.         public float speed = 0.05f;
  29.         // -------------------------------------------------------------------------------------------------------------------------------------------
  30.        
  31.         public Transform cubeSpawnPoint;
  32.        
  33.         private int initialBlockAmount = 12;
  34.         private int blocksAhead = 12;
  35.         private int blockCreateAmount = 3;
  36.            
  37.        
  38.         // INIT
  39.         // -------------------------------------------------------------------------------------------------------------------------------------------
  40.         void Start () {
  41.                    
  42.             BlockStorage = new GameObject("Block Storage"); // Instantiates the block storage object
  43.            
  44.             // Error handeling for if thhe cubeSpawnPoint transform was not chosen  (avoids null reference)
  45.             if(cubeSpawnPoint == null){
  46.                 cubeSpawnPoint = this.transform;
  47.             }
  48.            
  49.             // Instantiates n amount of blocks (based on the initialBlockAmoun variable) just to populate the level at the start of game
  50.             for (int i = 0; i < initialBlockAmount; i++) {
  51.                 CreateBlock();
  52.             }
  53.                    
  54.    
  55.         }
  56.         // -------------------------------------------------------------------------------------------------------------------------------------------
  57.        
  58.         void Update () {
  59.            
  60.             /*  This if statement checks to see if the players position ( specifically, this game object) has got the size of the cube * blockAhead
  61.              *   distance away form the last block. Essentially blocksAhead variable tests to see if the player is that many blocks away from hitting the end
  62.              * It then creates an amount of blocks based on blockCreateAmount variable ... so 3 which seems to be enough at most speeds.
  63.              */
  64.            
  65.             if(this.transform.position.z >= lastBlockPosition.z - (blocksInLevel[blocksInLevel.Count-1].transform.lossyScale.z * blocksAhead)){
  66.                 for (int i = 0; i < blockCreateAmount; i++) {
  67.                     CreateBlock();
  68.                 }
  69.                
  70.             }
  71.         }
  72.        
  73.         /*
  74.          * This cretes a single block and adds it to the blocksInLevel List <GameObject> : This is done to keep an eye on the order and allow you to remove
  75.          * Blocks based on their order or loop blocks or slice parts out of the list for isolated selection.
  76.          */
  77.        
  78.         void CreateBlock (){
  79.             try {
  80.                 GameObject tmpCube = (GameObject)Instantiate(prefabs[ Random.Range(0, prefabs.Count - 1) ] );
  81.                 tmpCube.transform.parent = BlockStorage.transform;
  82.                 blocksInLevel.Add(tmpCube);
  83.                 tmpCube.transform.position = cubeSpawnPoint.position;          
  84.                 tmpCube.transform.Translate(0.0f ,0.0f , lastBlockPosition.z + tmpCube.transform.localScale.z);
  85.                 lastBlockPosition = tmpCube.transform.position;
  86.                
  87.             } catch{
  88.                 print("There is an issue with the prefabs array, make sure it has at is populated and there are no empty slots.");
  89.             }
  90.            
  91.         }
  92.        
  93.        
  94.         // THIS IS FOR DEMONSTRATION YOU MAY REMOVE THIS
  95.         // -------------------------------------------------------------------------------------------------------------------------------------------
  96.         void FixedUpdate()
  97.         {
  98.             //this
  99.             rigidbody.AddForce(transform.forward * speed, ForceMode.VelocityChange);
  100.         }
  101.         // -------------------------------------------------------------------------------------------------------------------------------------------
  102.    
  103.        
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement