Skip to content

Instantly share code, notes, and snippets.

@ZeroCool5254
Last active Mar 25, 2021
Embed
What would you like to do?
This is the code of my Space Shooter Course as covered and updated in my Medium Posts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField, Header("Enemy Settings")]
private float _speed = 4.0f;
void Start()
{
}
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y <= -5.4f)
{
float randomX = Random.Range(-9.0f, 9.0f);
transform.position = new Vector3(randomX, 7, 0);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Player player = other.transform.GetComponent<Player>();
if (player != null)
{
player.Damage();
}
Destroy(this.gameObject);
}
if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
public void Destruct()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour
{
[SerializeField, Header("Laser Settings")]
private float _speed = 8.0f;
void Update()
{
transform.Translate(Vector3.up * _speed * Time.deltaTime);
if (transform.position.y >= 7)
{
Destroy(this.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField, Header("Player Settings")]
private float _speed = 5f;
[SerializeField]
private int _lives = 3;
[SerializeField, Header("Laser Settings")]
private GameObject _laserPrefab;
[SerializeField]
private GameObject _laserContainer;
[SerializeField]
private float _fireRate = 0.5f, _canFire = -1f;
[SerializeField, Header("TripleShot Settings")]
private GameObject _tripleShotPrefab;
[SerializeField]
private bool _tripleShotActive = false;
private SpawnManager _spawnManager;
void Start()
{
transform.position = new Vector3(0, 0, 0);
_spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
if (_spawnManager == null)
{
Debug.LogError("SpawnManager is Null");
}
}
void Update()
{
Movement();
Fire();
}
//Movement Systems
private void Movement()
{
//Player Movement
float hPos = Input.GetAxis("Horizontal");
float vPos = Input.GetAxis("Vertical");
transform.Translate(new Vector3(hPos, vPos, 0) * _speed * Time.deltaTime);
//Player Screen Clamping
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.7f, 0), 0);
if (transform.position.x > 10)
{
transform.position = new Vector3(-10, transform.position.y, 0);
}
else if (transform.position.x < -10)
{
transform.position = new Vector3(10, transform.position.y, 0);
}
}
//Firing Systems
private void Fire()
{
if (Input.GetButton("Fire1") && Time.time > _canFire)
{
_canFire = Time.time + _fireRate;
if (_tripleShotActive == true)
{
GameObject tripleShotLaser = Instantiate(_tripleShotPrefab, transform.position + new Vector3(0, 1.1f, 0), Quaternion.identity);
tripleShotLaser.transform.parent = _laserContainer.transform;
}
else
{
GameObject newLaser = Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.1f, 0), Quaternion.identity);
newLaser.transform.parent = _laserContainer.transform;
}
}
}
public void ActivateTripleShot()
{
_tripleShotActive = true;
StartCoroutine(TripleShotCooldownRoutine());
}
IEnumerator TripleShotCooldownRoutine()
{
yield return new WaitForSeconds(5.0f);
_tripleShotActive = false;
}
//Damage Systems
public void Damage()
{
_lives--;
if (_lives < 1)
{
_spawnManager.OnPlayerDeath();
Destroy(this.gameObject);
Debug.Log("You died! Try again");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
[SerializeField, Header("Spawn Settings")]
private GameObject _enemyPrefab;
[SerializeField]
private GameObject _enemyContainer;
private bool _stopSpawning = false;
[SerializeField, Header("Timing settings")]
private float _spawnTimer = 5.0f;
void Start()
{
StartCoroutine(SpawnRoutine());
}
public void OnPlayerDeath()
{
_stopSpawning = true;
foreach (var enemy in _enemyContainer.GetComponentsInChildren<Enemy>())
{
enemy.Destruct();
}
//browny points
}
IEnumerator SpawnRoutine()
{
while (_stopSpawning == false)
{
float randomX = Random.Range(-9.0f, 9.0f);
Vector3 posToSpawn = new Vector3(randomX, 7, 0);
GameObject newEnemy = Instantiate(_enemyPrefab, posToSpawn, Quaternion.identity);
newEnemy.transform.parent = _enemyContainer.transform;
yield return new WaitForSeconds(_spawnTimer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment