Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- /*
- Spawn & Reuse Objects
- Updated: 2012-07-18
- */
- enum PLAYERAXIS { x,y,z }
- var obstacles : GameObject[]; //Obstacles
- var obstacles1;
- var obstacles2;
- var obstacles3;
- var playerTransform : Transform; //Player's transform (we use this to set position of the spawn area)
- var totalSpawnObjects : int = 50; //The amount of objects that is allowed in the scene
- var spawnPause : float = .2; //The pause between spawns
- var spawnAreaPosition : Vector3 = new Vector3(0,0,0); //The position of the spawn area
- 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)
- var followPlayerOnAxisDistance : PLAYERAXIS = PLAYERAXIS.z; //The axis the spawn area follow the player in distance
- var distanceFromPlayer : int = 200; //The distance of the spawn are to the player
- private var spawnTimeScheduler : float = .0;
- private var spawnPointer : int = 0;
- // Static variables for cached components of the objects below (we use this to access the spawned objects outside the script easily)
- static var objects : GameObject[];
- static var objectsTransform : Transform[];
- static var objectsRenderer : Renderer[];
- function Start ()
- {
- objects = new GameObject[totalSpawnObjects];
- objectsTransform = new Transform[totalSpawnObjects];
- objectsRenderer = new Renderer[totalSpawnObjects];
- if (playerTransform==null) playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
- }
- function Update ()
- {
- if (Time.time>=spawnTimeScheduler) Spawn();
- }
- function Spawn ()
- {
- //Position the spawn area
- spawnAreaPosition = playerTransform.position;
- switch (followPlayerOnAxisDistance)
- {
- case PLAYERAXIS.x: spawnAreaPosition.x += distanceFromPlayer; break;
- case PLAYERAXIS.y: spawnAreaPosition.y += distanceFromPlayer; break;
- case PLAYERAXIS.z: spawnAreaPosition.z += distanceFromPlayer; break;
- }
- //Bake the spawn position into a variable
- var spawnPosition : Vector3 = Vector3
- (
- spawnAreaPosition.x - Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2),
- spawnAreaPosition.y - Random.Range(-spawnAreaSize.y / 2, spawnAreaSize.y / 2),
- spawnAreaPosition.z - Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2)
- );
- //Spawn new object
- if (objects[spawnPointer]!=null)
- {
- //Reuse the object
- objectsTransform[spawnPointer].position = spawnPosition;
- }
- else
- {
- //Instantiate the object
- var thisObject : GameObject = Instantiate(obstacles[0,1,2], spawnPosition, Quaternion.identity);
- CurrentSpawnedComponentCache(spawnPointer, thisObject.gameObject, thisObject.transform, thisObject.renderer);
- }
- if (spawnPointer++ >= totalSpawnObjects - 1) spawnPointer = 0;
- spawnTimeScheduler = Time.time + spawnPause;
- }
- //Cache the components of spawned objects
- function CurrentSpawnedComponentCache (i : int, g : GameObject, t : Transform, r : Renderer)
- {
- objects[i] = g;
- objectsTransform[i] = t;
- objectsRenderer[i] = r;
- }
- //Draw the spawn area in Scene view
- function OnDrawGizmos ()
- {
- Gizmos.color = Color.yellow;
- Gizmos.DrawWireCube (spawnAreaPosition, spawnAreaSize);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement