My History of Game Programming (Part 4 – Unity/C#)

In 2014 I added Unity© software to my life and started a 2d side scrolling platformer with a friend as another learning experience.  It is swift working with the engine after having to program in all the basics in past projects. The basics being the player controller, collisions, animation controller, camera, so on and so forth. If you are new to creating video games then I very much recommend Unity© software. It took some time to learn the ins and outs of the engine but now I feel as if I can accomplish a plethora of tasks within it and do so in a speedy manner.

Below is a gif of the first project I did with Unity© software.  The most unique part of the project is the ledge grabbing. It works by having a box collider 2d that is slightly larger then the players near the edges that are grabbable and when that collision box contains the player, the players position is set to the corner, the players state is no longer effected by gravity and the animation is set to the grabbing animation.

The sprite sheet is from an artist on DeviantArt and was being used just for internal testing purposes.

GIF

After the ledge grab project my friend suggested we make an old school feeling 3d multiplayer FPS arena game. Although it did not last long, we did end up with enough of the game where we enjoyed spending time battling each other in it. It had a multiplayer driven by Photon Unity Networking Free which is on the asset store and worked great for our purposes. Photon is easy to use and is a great solution for a specific group of multiplayer game types. The game has one level, which was created by my friend whom was following a tutorial on youtube. I added a teleporter (with telefragging of course), a health pickup, hitscan weapon, projectile weapon, melee weapon, hit indicator, blood splatter, text popup when someone dies and I modified the FPS controller to do double jumps and to do quick dodges if you double tapped a direction.

Below is a gif of the gameplay. My friend added the skybox, which I believe is from an old game but I do not remember which.

GIF

Below is the class that handles the muzzle flash when shooting the sub machine gun in the game. It uses another class which I named MuzzleFlashInfo to store the variables that each muzzle flash uses. When the player presses fire it will set a muzzle flash, in the array of muzzle flashes, to active for each shot and then fade out over time by changing the opacity of the sprite renderer attached to the muzzle flash game object. After the time the muzzle flash exists is up then the muzzle flash is set to inactive and it’s starting variables are reset so it can be used again.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class MuzzleFlashInfo
{
    [System.NonSerialized]
    public float timeExistsCount = 0; //can be set in inspector
    public float timeExists = 500; //time in milliseconds
    public float delayBeforeOpacityStart = 125; //can be set in inspector
    [System.NonSerialized]
    public bool activeFlash = false;
    public GameObject muzzleFlashGO; //set in inspector
    public float startingOpacity = .75f; //can be set in inspector
}

public class MuzzleFlashSMG : MonoBehaviour
{
    private MuzzleFlashInfo[] muzzleFlashInfo; //set in inspector
    [System.NonSerialized]
    private bool shot = false;
    int muzzleFlashIndex = 0;

    // Use this for initialization
    void Start()
    {
        //set the starting opacity of each muzzle flash
        for (int i = 0; i < muzzleFlashInfo.Length - 1; i++)
        {
            //before getting render we can add a check to make sure a SpriteRenderer is attached and if not give Debug.log output
            muzzleFlashInfo[i].muzzleFlashGO.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, muzzleFlashInfo[i].startingOpacity);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (shot)
        {
            muzzleFlashInfo[muzzleFlashIndex].activeFlash = true;
            muzzleFlashInfo[muzzleFlashIndex].muzzleFlashGO.GetComponent<SpriteRenderer>().enabled = true;
            shot = false;
            muzzleFlashIndex++;
            if (muzzleFlashIndex > muzzleFlashInfo.Length - 1)
                muzzleFlashIndex = 0;
        }

        //run the logic of each active muzzle flash
        for (int i = 0; i < muzzleFlashInfo.Length; i++)
        {
            if (muzzleFlashInfo[i].activeFlash)
            {
                muzzleFlashInfo[i].timeExistsCount += Time.deltaTime * 1000;

                //starts the opacity change after a delay
                if (muzzleFlashInfo[i].timeExistsCount >= muzzleFlashInfo[i].delayBeforeOpacityStart)
                {
                    muzzleFlashInfo[i].muzzleFlashGO.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f - 
                        ((muzzleFlashInfo[i].startingOpacity - muzzleFlashInfo[i].timeExistsCount / 1000f)));
                }
                if (muzzleFlashInfo[i].timeExistsCount >= muzzleFlashInfo[i].timeExists)
                {
                    //reset muzzle flash variables and disable it for use later
                    muzzleFlashInfo[i].timeExistsCount = 0;
                    muzzleFlashInfo[i].muzzleFlashGO.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, muzzleFlashInfo[i].startingOpacity);
                    muzzleFlashInfo[i].muzzleFlashGO.GetComponent<SpriteRenderer>().enabled = false;
                    muzzleFlashInfo[i].activeFlash = false;
                }
            }
        }
    }

    //when a bullet is fired this is called
    public void ShowMuzzleFlash()
    {
        shot = true;
    }
}