Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

2D Games Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour


{
// Public variables for player and enemy objects
public GameObject player;
public GameObject enemy;

// Public variables for weapons


public GameObject[] weapons;
public int currentWeaponIndex;

// Public variable for map size


public int mapSize;

// Private variables for enemy spawning


private int currentEnemyCount;
private int maxEnemyCount = 10;

void Start()
{
// Spawn player in the center of the map
Instantiate(player, new Vector3(0, 0, 0), Quaternion.identity);

// Spawn weapons randomly throughout the map


for (int i = 0; i < weapons.Length; i++)
{
Vector3 randomPosition = new Vector3(Random.Range(-mapSize, mapSize),
0, Random.Range(-mapSize, mapSize));
Instantiate(weapons[i], randomPosition, Quaternion.identity);
}
}

void Update()
{
// Switch weapons with number keys or weapon wheel
if (Input.GetKeyDown(KeyCode.Alpha1))
{
currentWeaponIndex = 0;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
currentWeaponIndex = 1;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
currentWeaponIndex = 2;
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
currentWeaponIndex = 3;
}

// Fire weapon with left mouse click


if (Input.GetMouseButtonDown(0))
{
weapons[currentWeaponIndex].GetComponent<Weapon>().Fire();
}

// Spawn enemies randomly throughout the map


if (currentEnemyCount < maxEnemyCount)
{
Vector3 randomPosition = new Vector3(Random.Range(-mapSize, mapSize),
0, Random.Range(-mapSize, mapSize));
Instantiate(enemy, randomPosition, Quaternion.identity);
currentEnemyCount++;
}
}

// Function for enemy death


public void EnemyKilled()
{
currentEnemyCount--;
}
}

RPG 2D Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RPG : MonoBehaviour


{
// Public variables for explosion effect and area of effect
public GameObject explosionEffect;
public float areaOfEffect = 5f;

// Public variable for damage


public int damage = 50;

// Private variables for firing and reloading


private bool canFire = true;
private float reloadTime = 3f;

// Function for firing RPG


public void Fire(Vector2 direction)
{
// Check if RPG can fire
if (!canFire)
{
return;
}

// Create explosion effect


Instantiate(explosionEffect, transform.position, Quaternion.identity);

// Get all colliders in area of effect


Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position,
areaOfEffect);

// Damage all colliders in area of effect


foreach (Collider2D collider in colliders)
{
// Check if collider is an enemy
if (collider.CompareTag("Enemy"))
{
collider.GetComponent<Enemy>().TakeDamage(damage);
}

// Check if collider is an object that can be destroyed


if (collider.CompareTag("Breakable"))
{
Destroy(collider.gameObject);
}
}

// Set RPG to not fire and start reload timer


canFire = false;
StartCoroutine(Reload());
}

// Function for reloading RPG


IEnumerator Reload()
{
yield return new WaitForSeconds(reloadTime);
canFire = true;
}
}

You might also like