• umutefiloglu@outlook.com
  • Ankara, Turkey

Space-Shooter-2D [Updated in GitHub on 22 Dec 2018]

2D Space Shooter Game

All cosmetic elements (sounds, space ship, etc.) are also created by me too, amateurish.

GitHub link of the project is here.

ss

Some Codes From The Project

PlayerController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    //speed of movement
    public float playerSpeed;
    //first element holds negative values -x,-y
    //second element holds positive values x,y
    public Vector2 []mapBorders;
    //shooting speed
    public float shootingSpeed;
    //is player shooting?
    public bool isShooting;
    //projectile object
    public GameObject projectile;

	// Use this for initialization
	void Start () {
        StartCoroutine(SpawnProjectile(shootingSpeed));
    }
	
	// Update is called once per frame
	void Update () {
        
    }

    //physical calculations
    void FixedUpdate()
    {
        PlayerMovement();
        PlayerShootingTimer();
    }

    //function that controls player's movement
    private void PlayerMovement()
    {
        //left movement
        if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x > mapBorders[0].x)
        {
            transform.Translate(-playerSpeed, 0, 0);
        }

        //right movement
        if (Input.GetKey(KeyCode.RightArrow) && transform.position.x < mapBorders[1].x)
        {
            transform.Translate(playerSpeed, 0, 0);
        }

        //down movement
        if (Input.GetKey(KeyCode.DownArrow) && transform.position.y > mapBorders[0].y)
        {
            transform.Translate(0, -playerSpeed, 0);
        }

        //up movement
        if (Input.GetKey(KeyCode.UpArrow) && transform.position.y < mapBorders[1].y)
        {
            transform.Translate(0, playerSpeed, 0);
        }
    }
    
    //function that controls player's shooting time
    private void PlayerShootingTimer()
    {
        if (Input.GetKey(KeyCode.LeftControl))
        {
            isShooting = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            isShooting = false;
        }
    }
    
    //function spawning projectile
    IEnumerator SpawnProjectile(float shootingSpeed)
    {
        while (true)
        {
            if (isShooting == true)
            {
                Instantiate(projectile, new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z), Quaternion.identity);
                yield return new WaitForSeconds(shootingSpeed);
            }
            else
            {
                yield return null;
            }
        }
    }
}
ProjectileController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileController : MonoBehaviour {
    //projectile transform speed
    public float transformSpeed;
    public Vector2[] mapBorders;
    public GameObject ExplosionEffect;

    // Use this for initialization
    void Start () {
        mapBorders = GameObject.Find("Player").GetComponent<PlayerController>().mapBorders;
        GameObject.Find("GameController").GetComponent<SoundController>().isProjectileFired = true;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    //fixed frame
    private void FixedUpdate()
    {
        TransformProjectile();

        //check if projectile should be destroyed or not
        if (transform.position.y >= mapBorders[1].y + 1f)
        {
            Destroy(gameObject);
        }
    }

    //projectile transform
    private void TransformProjectile()
    {
        transform.Translate(0, transformSpeed, 0);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "SquareEnemy")
        {
            GameObject.FindGameObjectWithTag("GameController").GetComponent<ScoreCalculator>().score += 100;
            Instantiate(ExplosionEffect, new Vector3(transform.position.x, transform.position.y + 2f, transform.position.z), Quaternion.identity);
            GameObject.Find("GameController").GetComponent<SoundController>().isSquareEnemyKilled = true;
            Debug.Log("Square enemy was hit by projectile!");
            Destroy(collision.gameObject);
            Destroy(gameObject);
        }
    }
}