Geta-Ve

Tower Creation Script

Jul 24th, 2012
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. /*
  4.     Spawn & Reuse Objects  
  5.     Updated: 2012-07-18
  6. */
  7.  
  8. enum PLAYERAXIS { x,y,z }
  9.  
  10. var obstacles : GameObject[]; //Obstacles
  11. var obstacles1;
  12. var obstacles2;
  13. var obstacles3;
  14.  
  15. var playerTransform : Transform; //Player's transform (we use this to set position of the spawn area)
  16. var totalSpawnObjects : int = 50; //The amount of objects that is allowed in the scene
  17. var spawnPause : float = .2; //The pause between spawns
  18. var spawnAreaPosition : Vector3 = new Vector3(0,0,0); //The position of the spawn area
  19. var spawnAreaSize : Vector3 = new Vector3(200,0,50); //The size of the spawn area (we then randomly spawn inside this area in X, Y and Z)
  20. var followPlayerOnAxisDistance : PLAYERAXIS = PLAYERAXIS.z; //The axis the spawn area follow the player in distance
  21. var distanceFromPlayer : int = 200; //The distance of the spawn are to the player
  22.  
  23. private var spawnTimeScheduler : float = .0;
  24. private var spawnPointer : int = 0;
  25.  
  26. // Static variables for cached components of the objects below (we use this to access the spawned objects outside the script easily)
  27. static var objects : GameObject[];
  28. static var objectsTransform : Transform[];
  29. static var objectsRenderer : Renderer[];
  30.  
  31.  
  32. function Start ()
  33. {
  34.     objects = new GameObject[totalSpawnObjects];
  35.     objectsTransform = new Transform[totalSpawnObjects];
  36.     objectsRenderer = new Renderer[totalSpawnObjects];
  37.    
  38.     if (playerTransform==null) playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
  39. }
  40.  
  41. function Update ()
  42. {
  43.     if (Time.time>=spawnTimeScheduler) Spawn();
  44. }
  45.  
  46. function Spawn ()
  47. {
  48.     //Position the spawn area
  49.     spawnAreaPosition = playerTransform.position;
  50.    
  51.     switch (followPlayerOnAxisDistance)
  52.     {
  53.         case PLAYERAXIS.x: spawnAreaPosition.x += distanceFromPlayer; break;
  54.         case PLAYERAXIS.y: spawnAreaPosition.y += distanceFromPlayer; break;
  55.         case PLAYERAXIS.z: spawnAreaPosition.z += distanceFromPlayer; break;
  56.     }
  57.  
  58.     //Bake the spawn position into a variable
  59.     var spawnPosition : Vector3 = Vector3
  60.         (
  61.             spawnAreaPosition.x - Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2),
  62.             spawnAreaPosition.y - Random.Range(-spawnAreaSize.y / 2, spawnAreaSize.y / 2),
  63.             spawnAreaPosition.z - Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2)
  64.         );
  65.  
  66.     //Spawn new object
  67.     if (objects[spawnPointer]!=null)
  68.     {
  69.         //Reuse the object
  70.         objectsTransform[spawnPointer].position = spawnPosition;
  71.     }
  72.      
  73.         else
  74.         {
  75.             //Instantiate the object
  76.             var thisObject : GameObject = Instantiate(obstacles[0,1,2], spawnPosition, Quaternion.identity);
  77.            
  78.             CurrentSpawnedComponentCache(spawnPointer, thisObject.gameObject, thisObject.transform, thisObject.renderer);          
  79.         }
  80.        
  81.    
  82.     if (spawnPointer++ >= totalSpawnObjects - 1) spawnPointer = 0;
  83.     spawnTimeScheduler = Time.time + spawnPause;
  84. }
  85.  
  86. //Cache the components of spawned objects
  87. function CurrentSpawnedComponentCache (i : int, g : GameObject, t : Transform, r : Renderer)
  88. {
  89.     objects[i] = g;
  90.     objectsTransform[i] = t;
  91.     objectsRenderer[i] = r;
  92. }
  93.  
  94. //Draw the spawn area in Scene view
  95. function OnDrawGizmos ()
  96. {
  97.     Gizmos.color = Color.yellow;
  98.     Gizmos.DrawWireCube (spawnAreaPosition, spawnAreaSize);
  99. }
RAW Paste Data