Skip to content

Instantly share code, notes, and snippets.

@Liam-Angel
Last active October 31, 2025 09:17
Show Gist options
  • Save Liam-Angel/2acde264297a9b3b00cb8779a42d305b to your computer and use it in GitHub Desktop.
Save Liam-Angel/2acde264297a9b3b00cb8779a42d305b to your computer and use it in GitHub Desktop.
Liam Angel - Final Build
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ShootController : MonoBehaviour
{
public InputAction shootAction;
public InputAction aimAction;
public InputAction scrollAction;
public Transform firePoint;
public float fireRate;
public Gun rifleData;
public Gun shotgunData;
public Gun launcherData;
int currentGun;
bool shootReady = true;
void OnEnable()
{
scrollAction.performed += OnScrollPerformed;
shootAction.performed += OnShootPerformed;
shootAction.Enable();
aimAction.Enable();
scrollAction.Enable();
}
public void OnShootPerformed(InputAction.CallbackContext context)
{
Vector2 aimInput = aimAction.ReadValue<Vector2>();
float aimDirection = Mathf.Atan2(aimInput.x, aimInput.y) * Mathf.Rad2Deg;
firePoint.rotation = Quaternion.Euler(0f, 0f, aimDirection);
if (currentGun == 0 && shootReady == true)
{
Instantiate(rifleData.Bullet1, firePoint.position, firePoint.rotation);
shootReady = false;
StartCoroutine(ShootDelay(rifleData.fireRate));
}
if (currentGun == 1 && shootReady == true)
{
firePoint.Rotate(0f, 0f, shotgunData.spread * 2);
for(int i = 5; i>=1; i--)
{
Instantiate(shotgunData.Bullet1, firePoint.position, firePoint.rotation);
firePoint.Rotate(0f, 0f, -shotgunData.spread);
}
shootReady = false;
StartCoroutine(ShootDelay(shotgunData.fireRate));
}
if (currentGun == 2 && shootReady == true)
{
Instantiate(launcherData.Bullet1, firePoint.position, firePoint.rotation);
shootReady = false;
StartCoroutine(ShootDelay(launcherData.fireRate));
}
}
public void OnScrollPerformed(InputAction.CallbackContext context)
{
currentGun++;
if(currentGun > 2)
{
currentGun = 0;
}
}
IEnumerator ShootDelay(float delay)
{
yield return new WaitForSeconds(delay);
shootReady = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController1 : MonoBehaviour
{
public float damage;
public Rigidbody2D rb;
public Gun rifle;
public float bulletSpeed;
void Update()
{
rb.velocity = transform.right * bulletSpeed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemyDamage != null)
{
enemyDamage.TakeDamage(rifle.damage + PowerupScript.playerDamagePowerup);
}
if (enemy.tag != "Semisolid")
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController2 : MonoBehaviour
{
public Rigidbody2D rb;
public Gun shotgun;
void Update()
{
rb.velocity = transform.right * shotgun.bulletSpeed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemyDamage != null)
{
enemyDamage.TakeDamage(shotgun.damage + PowerupScript.playerDamagePowerup);
}
if (enemy.tag != "Semisolid")
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController3 : MonoBehaviour
{
public float damage;
public Rigidbody2D rb;
public Gun Launcher;
private void Awake()
{
rb.velocity = transform.right * Launcher.bulletSpeed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemy.tag != "Semisolid")
{
Instantiate(Launcher.Radius, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "NewGun")]
public class Gun : ScriptableObject
{
public float fireRate;
public float bulletSpeed;
public GameObject Bullet1;
public float spread;
public float damage;
public GameObject Radius;
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float lifetime = 3f;
public float damage = 1f;
public static string spawner;
private string newSpawner;
void Awake()
{
newSpawner = spawner;
if (newSpawner == "Player")
{
BulletSpawn.BulletCount += 1;
}
Invoke("Die", lifetime);
}
void Die()
{
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemyDamage != null && !enemy.CompareTag(newSpawner))
{
if (newSpawner == "Player")
{
enemyDamage.TakeDamage(damage + PowerupScript.playerDamagePowerup);
}
else
{
enemyDamage.TakeDamage(damage);
}
}
if (!enemy.CompareTag(newSpawner) && !(enemy.tag == "Semisolid")) {
Die();
}
}
private void OnDestroy()
{
if (newSpawner == "Player")
{
BulletSpawn.BulletCount -= 1;
}
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float lifetime = 3f;
public float damage = 1f;
public static string spawner;
private string newSpawner;
void Awake()
{
newSpawner = spawner;
if (newSpawner == "Player")
{
BulletSpawn.BulletCount += 1;
}
Invoke("Die", lifetime);
}
void Die()
{
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemyDamage != null && !enemy.CompareTag(newSpawner))
{
if (newSpawner == "Player")
{
enemyDamage.TakeDamage(damage + PowerupScript.playerDamagePowerup);
}
else
{
enemyDamage.TakeDamage(damage);
}
}
if (!enemy.CompareTag(newSpawner) && !(enemy.tag == "Semisolid")) {
Die();
}
}
private void OnDestroy()
{
if (newSpawner == "Player")
{
BulletSpawn.BulletCount -= 1;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Damage : MonoBehaviour
{
public float health = 5f;
public float PlayerInvuln = 0.65f;
public float EnemyInvuln = 0.1f;
private bool playerI;
private bool enemyI;
public void TakeDamage(float amount)
{
if (gameObject.tag == "Player" && playerI)
{
return;
}
else if (gameObject.tag == "Enemy" && enemyI)
{
return;
}
health -= amount;
Debug.Log(health);
if (health <= 0f)
{
if (gameObject.tag == "Player")
{
Camera.main.transform.parent = null; // Prevent an error when destroying the player object.
}
if (gameObject.tag == "Enemy")
{
TurretBehaviour enemy = gameObject.GetComponent<TurretBehaviour>();
if (enemy != null)
{
enemy.SpawnPowerup();
}
}
Destroy(gameObject);
}
if (gameObject.tag == "Player")
{
StartCoroutine(PlayerInvincibility());
}
else if (gameObject.tag == "Enemy")
{
StartCoroutine(EnemyInvincibility());
}
}
IEnumerator PlayerInvincibility()
{
playerI = true;
Debug.Log(playerI);
yield return new WaitForSecondsRealtime(PlayerInvuln);
playerI = false;
}
IEnumerator EnemyInvincibility()
{
enemyI = true;
yield return new WaitForSecondsRealtime(EnemyInvuln);
enemyI = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnterMenu : MonoBehaviour
{
public GameObject SoundMenu;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (SoundMenu.activeInHierarchy == true)
{
SoundMenu.SetActive(false);
}
else
{
SoundMenu.SetActive(true);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class ExplosionManager : MonoBehaviour
{
public float damage;
public Rigidbody2D rb;
public Gun Launcher;
public float delayTime = 1f;
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject enemy = collision.gameObject;
Damage enemyDamage = enemy.GetComponent<Damage>();
if (enemyDamage != null)
{
enemyDamage.TakeDamage(Launcher.damage + PowerupScript.playerDamagePowerup);
}
Invoke("destroy", delayTime);
}
void destroy()
{
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelChange : MonoBehaviour
{
public bool requiresEnemyDeath = true;
public string[] levelNames = { "Level 1", "Level 2" };
private int levelIndex = 0;
public bool checkEnemyDeath()
{
GameObject[] activeObjects = FindObjectsOfType<GameObject>();
foreach (GameObject g in activeObjects)
{
if (g.tag == "Enemy")
{
return false;
}
}
return true;
}
public bool checkNextScene()
{
if ((levelIndex + 1) < levelNames.Length)
{
return true;
}
return false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player" && checkEnemyDeath() && checkNextScene()) {
levelIndex += 1;
SceneManager.LoadScene(levelNames[levelIndex]);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingEnemyBehaviour : MonoBehaviour
{
public float enemyVelocity = 5f;
public float baseDelay = 1.2f;
public int enemyDamage = 2;
Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
StartCoroutine("MovementRoutine");
}
IEnumerator MovementRoutine()
{
while (true)
{
float rand = Random.value;
yield return new WaitForSecondsRealtime(baseDelay + rand);
enemyVelocity *= -1;
rb.velocity = new Vector2(enemyVelocity, rb.velocity.y);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Damage dmg = other.GetComponent<Damage>();
dmg.TakeDamage(enemyDamage);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
private Rigidbody2D playerBody;
public float moveSpeed = 10f;
public float jumpForce = 10f;
private float moveX;
private bool OnGround;
public LayerMask groundLayer; // Assigned in Unity GUI.
public BoxCollider2D GroundCollider; // Assigned in Unity GUI.
private bool DJump;
public float dashForce = 30f;
private bool isDash;
private bool canDash = true;
private float dashtime = 0.1f;
private float dashcool = 0.5f;
private bool isDashCool = false;
Vector2 dashdir;
void Start()
{
playerBody = GetComponent<Rigidbody2D>();
OnGround = true;
DJump = true;
}
// Update is called once per frame
void Update()
{
if (isDash)
{
playerBody.velocity = dashdir.normalized * dashForce;
return;
}
if (OnGround && isDashCool == false)
{
canDash = true;
}
moveX = Input.GetAxisRaw("Horizontal");
if (Input.GetKeyDown(KeyCode.Z) && OnGround)
{
playerBody.velocity = new Vector2(playerBody.velocity.x, jumpForce); // Add an initial y velocity to the player.
OnGround = false;
}
else if (Input.GetKeyDown(KeyCode.Z) && DJump && PowerupScript.djump)
{
playerBody.velocity = new Vector2(playerBody.velocity.x, jumpForce);
DJump = false;
}
if (Input.GetKeyDown(KeyCode.V) && canDash && PowerupScript.dash)
{
isDash = true;
isDashCool = true;
canDash = false;
dashdir = new Vector2(moveX, 0f).normalized;
if (dashdir == Vector2.zero)
{
dashdir = new Vector2(transform.localScale.x, 0f);
}
StartCoroutine(StopDash());
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (groundLayer == (1 << collision.gameObject.layer)) // Check if the objects are on the same layer (3, Ground) using bitmasks.
{
// GroundLayer will always be layer 3, or bitmask 8.
OnGround = true;
DJump = true;
canDash = true;
}
}
public void OnTriggerExit2D(Collider2D collision)
{
if (groundLayer == (1 << collision.gameObject.layer))
{
OnGround = false;
}
}
void FixedUpdate()
{
if (isDash)
{
return;
}
playerBody.velocity = new Vector2(moveX * moveSpeed, playerBody.velocity.y);
}
private IEnumerator StopDash()
{
yield return new WaitForSeconds(dashtime);
isDash = false;
yield return new WaitForSeconds(dashcool);
isDashCool = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerupScript : MonoBehaviour
{
// Start is called before the first frame update
public static int bulletCountPowerup = 0;
public static int playerDamagePowerup = 0;
public static int bulletSpeedPowerup = 0;
public static bool djump;
public static bool dash;
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
switch (gameObject.tag)
{
case "DamagePowerup":
playerDamagePowerup += 1;
break;
case "SpeedPowerup":
bulletSpeedPowerup += 1;
break;
case "CountPowerup":
bulletCountPowerup += 1;
break;
case "DJumpPowerup":
djump = true;
break;
case "DashPowerup":
dash = true;
break;
default:
break;
}
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SFXManager : MonoBehaviour
{
public static SFXManager instance;
[SerializeField] private AudioSource SFXObject;
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
public void playSFXClip(AudioClip audioClip, Transform spawnTransform, float volume)
{
AudioSource audioSource = Instantiate(SFXObject, spawnTransform.position, Quaternion.identity);
audioSource.clip = audioClip;
audioSource.volume = volume;
audioSource.Play();
float clipLength = audioSource.clip.length;
Destroy(audioSource.gameObject, clipLength);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class SoundMixerManager : MonoBehaviour
{
[SerializeField] private AudioMixer audioMixer;
public void SetMasterVolume(float level)
{
audioMixer.SetFloat("masterVolume", Mathf.Log10(level) * 20f);
}
public void SetSFXVolume(float level)
{
audioMixer.SetFloat("SFXVolume", Mathf.Log10(level) * 20f);
}
public void SetMusicVolume(float level)
{
audioMixer.SetFloat("musicVolume", Mathf.Log10(level) * 20f);
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditorInternal;
using UnityEngine;
public class TurretBehaviour : MonoBehaviour
{
// Start is called before the first frame update
static string spawnerTag = "Enemy";
public Transform FirePoint;
public GameObject bulletPrefab;
public float speed;
public GameObject player;
public GameObject[] powerups;
public float reloadTime = 5f;
public float fireTime = 0.5f;
void Start()
{
StartCoroutine(SpawnBullets());
}
IEnumerator SpawnBullets()
{
while (true)
{
float delay = Random.value;
yield return new WaitForSecondsRealtime(reloadTime + delay);
for (int i = 0; i < 3; i++)
{
if (player != null)
{
float rand_x = ((Mathf.RoundToInt(Random.value * 20) - 10) / 5);
float rand_y = ((Mathf.RoundToInt(Random.value * 20) - 10) / 5);
float dir_x = player.transform.position.x - gameObject.transform.position.x;
float dir_y = player.transform.position.y - gameObject.transform.position.y;
Vector2 actual_dir = new Vector2(dir_x + rand_x, dir_y + rand_y);
actual_dir.Normalize(); // Bullets shoot at a constant speed.
Shoot(actual_dir, FirePoint);
yield return new WaitForSecondsRealtime(fireTime + delay/10);
}
}
}
}
// Update is called once per frame
void Shoot(Vector2 dir, Transform spawn)
{
Bullet.spawner = spawnerTag;
GameObject newBullet = Instantiate(bulletPrefab, spawn.position, Quaternion.identity); // no rotation
Rigidbody2D bulletBody = newBullet.GetComponent<Rigidbody2D>();
if (bulletBody != null)
{
bulletBody.velocity = dir * speed;
}
}
public void SpawnPowerup()
{
float rand = Random.Range(0, powerups.Length);
int randIndex = Mathf.FloorToInt(rand);
Instantiate(powerups[randIndex], transform.position, Quaternion.identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment