0% found this document useful (0 votes)
6 views6 pages

Unity Enemy Spawner and Level Controller

The document contains a Unity script for a game that includes classes for spawning enemies, managing game levels, and controlling player health. The Spawner class generates enemies at random intervals within specified borders, while the LevelController checks for victory or defeat conditions based on enemy presence and player health. The Enemy class defines enemy behavior, including movement and attacking the player, with health management and speed adjustments based on the current level.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Unity Enemy Spawner and Level Controller

The document contains a Unity script for a game that includes classes for spawning enemies, managing game levels, and controlling player health. The Spawner class generates enemies at random intervals within specified borders, while the LevelController checks for victory or defeat conditions based on enemy presence and player health. The Enemy class defines enemy behavior, including movement and attacking the player, with health management and speed adjustments based on the current level.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

using System.

Collections;
using [Link];
using UnityEngine;

public class Spawner : MonoBehaviour


{
public Enemy enemyPrefab;

public Transform topBorder;


public Transform bottomBorder;

public float spawnIntervalMax = 3.5f;


public float spawnIntervalMin = 1f;

public float spawnTimer = 0f;

public int spawnCounter = 10;

public int spawnAddPerLevel = 5;

void Update()
{
if (spawnTimer > 0)
{
spawnTimer -= [Link];
}
else if (spawnCounter > 0)
{
Spawn();
}

}
public void Spawn()
{
spawnCounter--;

spawnTimer = [Link](spawnIntervalMin, spawnIntervalMax);

Vector2 randomPosition = new Vector2([Link].x,


[Link]([Link].y, [Link].y));

Instantiate(enemyPrefab, randomPosition, [Link]);


}
}
using [Link];
using [Link];
using UnityEngine;

public class LevelController : MonoBehaviour


{
public Spawner spawner;

public static bool finished = false;

public static int level = 1;

private void Start()


{
finished = false;
}

private void Update()


{
if(finished == false)
{
Check();
}
}

public void Check()


{
if ([Link] <= 0)
{
Enemy[] enemies = FindObjectsOfType<Enemy>();

if ([Link] <= 0)
{
Victory();
}
}

if([Link] <= 0)
{
Defeat();
}

public void Victory()


{
print("Victory!");
finished = true;
level += 1;
}

public void Defeat()


{
print("Defeat!");
finished = true;
level = 1;
}

}
using [Link];
using [Link];
using UnityEngine;

public class Corn : MonoBehaviour


{
public static Corn singleton;
public int health;

private void Awake()


{
singleton = this;
}

public void TakeDamage()


{
if (health > 0)
{
health -= 1;
}
}

}
using [Link];
using [Link];
using UnityEngine;

public class Enemy : MonoBehaviour


{
public int health;
public float speed = 1f;
float borderPosX;
public Animator animator;
public float shootInterval = 1f;
public float shootTimer = 0f;

public float speedPerLevel = 0.2f;

private void Start()


{
speed += speedPerLevel * [Link];

borderPosX = [Link].x;
}

private void Update()


{
if (shootTimer > 0)
{
shootTimer -= [Link];
}

float enemyPosX = [Link].x;

if (enemyPosX > borderPosX)


{
[Link]("isMoving", true);
[Link] += -[Link] * speed * [Link];

}
else
{
[Link]("isMoving", false);

if (shootTimer <= 0)
{
Attack();
shootTimer = shootInterval;
}
}
}

public void TakeDamage()


{
health -= 1;

if (health <= 0)
Destroy(gameObject);
}

public void Attack()


{
[Link]();
}
}

You might also like