0% found this document useful (0 votes)
26 views3 pages

Enemy Spawn 1

This document describes a script that spawns enemies over time at random spawn points. It increases spawn rate over time and spawns a boss enemy at the end. It also handles fading out the player character when all enemies are defeated.

Uploaded by

cine_jam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Enemy Spawn 1

This document describes a script that spawns enemies over time at random spawn points. It increases spawn rate over time and spawns a boss enemy at the end. It also handles fading out the player character when all enemies are defeated.

Uploaded by

cine_jam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using UnityEngine;

using System.Collections;

public class EnemySpawn1 : MonoBehaviour


{
public GameObject enemy; // The enemy prefab to be spawned.
public GameObject boss;
public float spawnTime; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this
enemy can spawn from.
private int i;
private bool end;
private GameObject lucy;
private Color color;
public AudioClip music2;
private AudioSource musicSource;
private int step;

// Use this for initialization


void Start()
{
// Call the Spawn function after a delay of the spawnTime and then continue
to call after the same amount of time.
InvokeRepeating("Spawn", spawnTime, spawnTime);
i = 0;// 0-80
step = 0;
end = false;
musicSource = AddAudioSource(music2, 1, true);
}

void Update()
{
if ((i == 15) && (step == 0))
{
spawnTime = spawnTime / 2;
CancelInvoke();
InvokeRepeating("Spawn", spawnTime, spawnTime);
step = 1;
}
if ((i == 35) && (step == 1))
{
step = 2;
}
if (((i >= 80) || (i == -1)) && (end == false))
{
if (i != -1)
{
CancelInvoke();
i = -1;
}
//Vector2 lookDir = Vector2.left;
//float velocity = 50;
if (!GameObject.FindGameObjectWithTag("Enemy"))
{
Instantiate(boss);
Destroy(GameObject.FindGameObjectWithTag("Music"));
musicSource.Play();
end = true;
}
}
if ((end == true) && (!GameObject.FindGameObjectWithTag("Enemy") && !
GameObject.FindGameObjectWithTag("Boss")))
{
lucy = GameObject.FindGameObjectWithTag("Player");

if (lucy != null)
{
color = lucy.GetComponent<SpriteRenderer>().color;
if (color.a > 0)
{
color.a -= (float)0.005;
lucy.GetComponent<SpriteRenderer>().color = color;
}
else
Destroy(lucy);
}
else
UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
//UnityEngine.SceneManagement.SceneManager.LoadScene("Level2");
}
}

void Spawn()
{
Vector2 lookDir = Vector2.left;
float velocity = 50;

// Find a random index between zero and one less than the number of spawn
points.
int spawnPointIndex = Random.Range(0, spawnPoints.Length);

// Create an instance of the enemy prefab at the randomly selected spawn


point's position and rotation.
GameObject newEnemy = Instantiate(enemy);
if ((i >= 35) && (i < 45))
newEnemy.SendMessage("AltStart", 1);
else if (i >= 45)
newEnemy.SendMessage("AltStart", 2);

newEnemy.transform.position =
spawnPoints[spawnPointIndex].transform.position;

Rigidbody2D enemyRb = newEnemy.GetComponent<Rigidbody2D>();


enemyRb.AddForce(lookDir.normalized * velocity);

i++;
}

public AudioSource AddAudioSource(AudioClip clip, float vol, bool loop)


{
AudioSource source = gameObject.AddComponent<AudioSource>();
source.clip = clip;
source.volume = vol;
source.loop = loop;
source.playOnAwake = false;
source.tag = "Music";
return source;
}
}

You might also like