Question by
stephen_george98 · May 18, 2016 at 09:13 PM ·
c#coroutinespawningienumeratorcombine
Any Way to Improve These Scripts or Increase Game Performance?
Hello all,
I have these 4 scripts that spawn my obstacles in my game. They are pretty similar to the "SpawningObstacles" script in the Unity Space Shooter Tutorial. I would just like to know if there is any way I could improve these scripts in any way? Maybe combine them or make them increase game performance? I attach these scripts to my parent GameObject for the logic in my obstacles. Here they are:
Obstacle 1:
using UnityEngine;
using System.Collections;
[System.Serializable] // Serialize this so it looks neater in the inspector
public class Obstacle1 // Hammer Obstacle
{
public GameObject hammer; // The first obstacle gameobject. This is attached in the inspector.
public Vector3 spawnHPosValues; // Where the first obstacle will be spawned at on the X,Y,Z plane.
public int hCount; // This is the count of the first obstacle in a given wave. //
public float hSpawnWait; // Time in seconds between next wave of obstacle 1.
public float hStartGameWait; // Time in seconds between when the game starts and when the first obstacle start spawning.
public float hWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 1 will spawn.
}
public class SpawnHammers : MonoBehaviour {
public Obstacle1 obstacle1; // Reference to the Obstacle 1 class //
void Start () {
StartCoroutine (SpawnHammer ()); // at the beginning of the game, start to function SpawnHammer() //
}
IEnumerator SpawnHammer () {
yield return new WaitForSeconds(obstacle1.hStartGameWait);
while (true)
{
for (int i = 0; i < obstacle1.hCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle1.spawnHPosValues.x, obstacle1.spawnHPosValues.x),
obstacle1.spawnHPosValues.y,
obstacle1.spawnHPosValues.z); // I choose where I spawn the obstacles on the X,Y,Z planes. Only the X
// plane will be random
Quaternion spawnRotation = Quaternion.identity; // Spawn with NO rotation
Instantiate (obstacle1.hammer, spawnPosition, spawnRotation); // Bring the object into the scene
yield return new WaitForSeconds(obstacle1.hSpawnWait); // Spawn a new obstacle
}
yield return new WaitForSeconds (obstacle1.hWaveSpawnWait); // Start the new wave of obstacles
}
}
}
Obstacle 4:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Obstacle4 // Barrel Obstacle
{
public GameObject barrel; // The fourth obstacle gameobject. This is attached in the inspector.
public Vector3 spawnBPosValues; // Where the fourth obstacle will be spawned at on the X,Y,Z plane.
public int bCount; // This is the count of the fourth obstacle in a given wave.
public float bSpawnWait; // Time in seconds between next wave of obstacle 4.
public float bStartGameWait; // Time in seconds between when the game starts and when the fourth obstacle start spawning.
public float bWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 4 will spawn.
}
public class SpawnBarrels : MonoBehaviour {
public Obstacle4 obstacle4;
void Start () {
StartCoroutine (SpawnBarrel ());
}
IEnumerator SpawnBarrel () {
yield return new WaitForSeconds(obstacle4.bStartGameWait);
while (true)
{
for (int i = 0; i < obstacle4.bCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle4.spawnBPosValues.x, obstacle4.spawnBPosValues.x),
obstacle4.spawnBPosValues.y,
obstacle4.spawnBPosValues.z);
Quaternion spawnRotation = Quaternion.Euler(0f,0f,90f); // rotate 90 degrees on the Z axis
Instantiate (obstacle4.barrel, spawnPosition, spawnRotation);
yield return new WaitForSeconds(obstacle4.bSpawnWait);
}
yield return new WaitForSeconds (obstacle4.bWaveSpawnWait);
}
}
}
Obstacle 3:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Obstacle3 // Cone Obstacle
{
public GameObject cone; // The third obstacle gameobject. This is attached in the inspector.
public Vector3 spawnCPosValues; // Where the third obstacle will be spawned at on the X,Y,Z plane.
public int cCount; // This is the count of the third obstacle in a given wave.
public float cSpawnWait; // Time in seconds between next wave of obstacle 3.
public float cStartGameWait; // Time in seconds between when the game starts and when the third obstacle start spawning.
public float cWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 3 will spawn.
}
public class SpawnCones : MonoBehaviour {
public Obstacle3 obstacle3;
void Start () {
StartCoroutine (SpawnCone ());
}
IEnumerator SpawnCone () {
yield return new WaitForSeconds(obstacle3.cStartGameWait);
while (true)
{
for (int i = 0; i < obstacle3.cCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle3.spawnCPosValues.x, obstacle3.spawnCPosValues.x),
obstacle3.spawnCPosValues.y,
obstacle3.spawnCPosValues.z);
Quaternion spawnRotation = Quaternion.Euler(0f,0f,90f); // rotate 90 degrees on the Z axis
Instantiate (obstacle3.cone, spawnPosition, spawnRotation);
yield return new WaitForSeconds(obstacle3.cSpawnWait);
}
yield return new WaitForSeconds (obstacle3.cWaveSpawnWait);
}
}
}
Obstacle 2:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Obstacle2 // Road Barrier Obstacle
{
public GameObject roadBarrier; // The second obstacle gameobject. This is attached in the inspector.
public Vector3 spawnRBPosValues; // Where the second obstacle will be spawned at on the X,Y,Z plane.
public int rbCount; // This is the count of the second obstacle in a given wave. //
public float rbSpawnWait; // Time in seconds between next wave of obstacle 2.
public float rbStartGameWait; // Time in seconds between when the game starts and when the second obstacle will start spawning.
public float rbWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 2 will spawn.
}
public class SpawnRoadBarriers : MonoBehaviour {
public Obstacle2 obstacle2;
void Start () {
StartCoroutine (SpawnRoadBarrier ());
}
IEnumerator SpawnRoadBarrier () {
yield return new WaitForSeconds(obstacle2.rbStartGameWait);
while (true)
{
for (int i = 0; i < obstacle2.rbCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle2.spawnRBPosValues.x, obstacle2.spawnRBPosValues.x),
obstacle2.spawnRBPosValues.y,
obstacle2.spawnRBPosValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (obstacle2.roadBarrier, spawnPosition, spawnRotation);
yield return new WaitForSeconds(obstacle2.rbSpawnWait);
}
yield return new WaitForSeconds (obstacle2.rbWaveSpawnWait);
}
}
}
I have a couple scripts I am working on that might solve what I asked here. Lemme know if you want to see them! Thank you :)
Comment