topical media & game development
lib-unity-tutorial-m2h-2-Assets-Game-5-Shooting-gallery-Scripts-ObjectSpawner.cs / cs
using UnityEngine;
using System.Collections;
public class MovingObject{
public DirectionEnum direction; //-1 or 1;
public Transform transform;
public MovingObject(DirectionEnum dir, Transform trans)
{
direction = dir;
transform = trans;
}
}
[System.Serializable]
public enum DirectionEnum{left = -1, right= 1}
public class ObjectSpawner : MonoBehaviour {
public Transform objectPrefab;
public DirectionEnum spawnDirection = DirectionEnum.right;
public static ObjectSpawner SP;
private float farLeft;
private float farRight;
private float lastSpawnTime;
private float spawnInterval;
void Awake () {
SP = this;
spawnInterval = Random.Range(3.5f, 5.5f);
lastSpawnTime = Time.time + Random.Range(0.0f, 1.5f);
}
void Update () {
//Spawn new object..
if ((lastSpawnTime + spawnInterval) < Time.time)
{
SpawnObject();
}
}
void SpawnObject()
{
lastSpawnTime = Time.time;
spawnInterval *= 0.99f;//Speed up spawning
DirectionEnum direction = spawnDirection; //-1 or 1
Transform newObj = (Transform)Instantiate(objectPrefab, transform.position, transform.rotation);
MovingObject movObj = new MovingObject(direction, newObj);
GameManager.SP.AddTarget( movObj );
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.