Archive for the ‘Unity’ Category.

3 "S"’s RPG Sample – Step 1

So like any game development project, the first steps we’ll take in implementing our 3 “S”‘s game will be putting together a design document and starting the shell of our actual game. Since we’ll be developing the game using Unity (shocker, I know) the first steps are pretty easy:

  • Create the project
  • Set up the Assets structure
  • Create the typical scenes

I have to assume that you have at least some basic knowledge about using Unity. If I had to start from the very beginning, we’d never actually finish building a game. The game interface and visuals of the game will be very simple as that’s not what we’re concentrating on here. Getting a working game completed is more important than a visually stunning game that never gets finished.

Start a new Unity project, select the 2D mode, give it a name. After Unity starts, create the following folders:

  • Audio
  • Prefabs
  • Scenes
  • Scripts
  • Sprites

In the Scenes folder create the following scenes:

  • CreateCharacter
  • Game
  • MainMenu

Open the Menu scene and add a couple of buttons:

  • New Game
  • Load Game
  • Options
  • Exit

We’ll look into what it takes to start a new game in a bit. For the Load Game menu, it’s up to you how you want to handle saving games and this will influence how the loading of games works. If you want to allow the player to save at any time with an unlimited number of saves, you could potentially have a lot of save files to deal with. You probably don’t want to allow this however. It’s more typical to give the player a limited amount of saves that he can overwrite. This is much easier to deal with when constructing your Load Game scene, although the theory is the same as if you were allowing an unlimited amount of save files. The player just ends up with a smaller list to scroll through.

What options you allow the player to tweak is up to you. At the very least, however, you’ll want to give the player some options for audio and video. Turning on and off and adjusting the volume level of audio is pretty standard. Setting the resolution and allowing the player to toggle between full-screen and windowed mode is the other usual group of option settings.

We need to add some scripts to hook up the basic UI elements. I usually name my scripts for this the same as the scene they’re used in. If you want to append “Scene” to a script to be more descriptive that’s fine. Add C# files for the MainMenu, Options, and Game scene.

The MainMenu script is pretty simple. It just transitions to the appropriate scene for the button clicked:

using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour {

    public GameObject ButtonPanel;
    public GameObject LoadGamePanel;

        void Start ()
    {
            //check to see if game has already been started and adjust buttons accordingly
       
        }
       
    public void NewGameButtonClick()
    {
        SceneManager.LoadScene("CreateCharacter");
    }

    public void LoadGameButtonClick()
    {
        LoadGamePanel.SetActive(true);
        ButtonPanel.SetActive(false);
    }

    public void OptionsButtonClick()
    {
        SceneManager.LoadScene("Options");
    }

    public void ExitButtonClick()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }

    public void SavedGamesCancelButtonClick()
    {
        LoadGamePanel.SetActive(false);
        ButtonPanel.SetActive(true);
    }

    public void SavedGamesOKButtonClick()
    {

    }
}

Hook each button to the appropriate method. We’ll be adding more to this for loading saved games, but this will do for now.

Open the Options scene and add an OK and Cancel button, add the following to the Options script, then hook the buttons to the methods:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Options : MonoBehaviour {

    private GameOptions _options;

    void Start()
    {
        //load current settings

    }

    public void OKButtonClick()
    {
        //save changed settings

        SceneManager.LoadScene("Menu");
    }

    public void CancelButtonClick()
    {
        SceneManager.LoadScene("Menu");
    }
}

We’ll need to do the same thing for the CreateCharacter scene. As the player will be performing a number of steps to fill in all of the character info, we’ll add Next and Previous buttons as well:

Note that the OK button has been disabled. It will only enable once all of the steps for creating the character have been completed.

The code to handle navigation for the buttons is pretty simple at this point:

using UnityEngine;
using UnityEngine.UI;

public class CreateCharacter : MonoBehaviour {

    private int _curStep;

    public Button NextButton;
    public Button PreviousButton;

        void Start ()
    {
        _curStep = 0;  
        }
       
        public void NextStepButtonClick()
    {
        _curStep++;
        if(_curStep == 3)
        {
            PreviousButton.interactable = true;
            NextButton.interactable = false;
        }

        ProcessStep();
    }

    public void PreviousStepButtonClick()
    {
        _curStep--;
        if (_curStep == 0)
        {
            PreviousButton.interactable = false;
            NextButton.interactable = true;
        }

        ProcessStep();
    }

    private void ProcessStep()
    {
        switch(_curStep)
        {
            case 0:
                {

                    break;
                }
            case 1:
                {

                    break;
                }
           case 2:
                {

                    break;
                }
            case 3:
                {

                    break;
                }      
        }
    }
}

We’ll start with 4 steps and can add more later if needed. The ProcessSteps method will simply toggle panels for each step as needed.

The last piece we’ll start on is the pause menu so we have a way to get out of the Game scene. This is a simple Panel with some buttons:

In the Game script is the typical button handler:

public class Game : MonoBehaviour
{

    public GameObject PausePanel;
    private bool _gamePaused;


        // Use this for initialization
        void Start ()
    {
               
        }
       
        // Update is called once per frame
        void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if(_gamePaused)
            {
                ResumeButtonClick();
            }
            else
            {
                PausePanel.SetActive(true);
                _gamePaused = true;
            }
        }
    }

    public void ResumeButtonClick()
    {
        PausePanel.SetActive(false);
        _gamePaused = false;
    }

    public void SaveGameButtonClick()
    {

    }

    public void ExitButtonClick()
    {
        SceneManager.LoadScene("MainMenu");
    }
}

We check each frame to see if the Esc key has been pressed and, if so, toggle the PausePanel based on whether or not the game is already paused.

This is the basic skeleton for our game that allows navigation between the current scenes. We’ll add more to this soon, but we need to do some work on the first two “S”’s before that happens. That’s coming up next.

So Many Stars

While many game services and platforms have built-in ratings system, there may come a time when you, like me currently, need a way to allow a user to rate your game using a standard 5 star system. I took a bit of time today to put together a scene to handle this.

First I created filled and outline star graphics:

 

Next I created a scene and laid out a couple of buttons and the 5 star outlines:

The stars are Image UI components. The OK button is not interactable until a rating is selected.

Next I added some event triggers to the stars:

The only unusual thing about this is I added a parameter to the standard event handler:

public void StarEnter(GameObject obj)

public void StarExit(GameObject obj)

public void StarClick(GameObject obj)

Since I’m using the same 3 events for all 5 stars I needed a way to tell which star is being handled. I didn’t want to have to go through some convoluted chunk of code to figure out which star the mouse was over. This would have involved doing a raycast from the mouse location and seeing it the ray hit something, then figuring out which star it hit. Once I set up the empty events in my script the Unity UI automatically recognized the need for a GameObject parameter.

Here’s the entire Rating class:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;

 

public class Rating : MonoBehaviour {

 

    public Image Star1;
    public Image Star2;
    public Image Star3;
    public Image Star4;
    public Image Star5;

 

    public Sprite Star;
    public Sprite StarOutline;

 

    private int _ratingSelected = 0;

 

    public Button OKButton;

 

    void Start()
    {
        //check for existing rating and disable if exists
        if (PlayerPrefs.HasKey("Rating"))
        {
            StarEnter(GameObject.Find(PlayerPrefs.GetInt("Rating").ToString() + "Star"));

 

            _ratingSelected = PlayerPrefs.GetInt("Rating");
        }
    }

 

    public void StarEnter(GameObject obj)
    {
        if (_ratingSelected == 0)
        {
            switch (obj.name)
            {
                case "1Star":
                {
                    Star1.sprite = Star;

 

                    break;
                }
                case "2Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;

 

                    break;
                }
                case "3Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;

 

                    break;
                }
                case "4Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;
                    Star4.sprite = Star;

 

                    break;
                }
                case "5Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;
                    Star4.sprite = Star;
                    Star5.sprite = Star;

 

                    break;
                }
            }
        }

 

    }

 

    public void StarExit(GameObject obj)
    {
        if (_ratingSelected == 0)
        {
            switch (obj.name)
            {
                case "1Star":
                {
                    Star1.sprite = StarOutline;

 

                    break;
                }
                case "2Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;

 

                    break;
                }
                case "3Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;

 

                    break;
                }
                case "4Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;
                    Star4.sprite = StarOutline;

 

                    break;
                }
                case "5Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;
                    Star4.sprite = StarOutline;
                    Star5.sprite = StarOutline;

 

                    break;
                }
            }
        }
    }

 


    public void StarClick(GameObject obj)
    {
        _ratingSelected = Convert.ToInt32(obj.name.Substring(0, 1));
        OKButton.interactable = true;
    }

 

    public void OKButton_Click()
    {
        //save rating
        PlayerPrefs.SetInt("Rating", _ratingSelected);
        Application.LoadLevel("Menu");
    }

 

    public void CancelButton_Click()
    {
        Application.LoadLevel("Menu");
    }
}
}

Since we don’t want the player to be able to change a rating once it’s made, the first thing we do is check to see if the player has already rated the game. I’m storing this in the PlayerPrefs for convenience sake. Obviously it should be in a place a little more secure. If the player has already rated the game, we call the StarEnter handler, passing the control for that number of stars, which sets the related number of stars. We then set the _ratingSelected member with the value from the PlayerPrefs key. Since the event handlers don’t do anything if the _ratingSelected member has a value other than 0, this effectively disables them.

The StarExit handler resets the images to the star outline. We could have simply not passed a parameter to it and just reset all of the stars, but I like the consistency of having a parameter for all 3 handlers.

If the player clicks a star, the _ratingSelected member is set and the OK button enabled, allowing the player to save the rating.

Pretty straightforward. If you use this as is, you’ll have to have a scene named “Menu” and Image controls with the names I used or you’ll get an error.

Feel free to comment – is this useful? Would you have done something different? Am I wasting my time here? 🙂

Rolling the Dice With Unity

No, I’m not talking about taking a chance using Unity to do game development. That decision was made once MS stopped caring about XNA 🙁 (obligatory links here and here on it’s impending demise). I’m talking about having rolling dice in your game. In this case it’s just one die and it’s 2D not 3D. Still, it might prove useful to some people so here we go.

So I’m working on a 2D board game that uses a die to determine the number of squares a player moves. I wanted to show the player something so they’d know the game is rolling a die rather than just generating a random number and showing it. Turns out it’s fairly easy to do using Unity.

First off, I needed some pictures of the 6 sides of a die:

     

I didn’t want to take up any space on the board and also wanted to let the player know in a rather obvious way that it was time to roll the die, so I decided to slide in a panel with the image of the die on it and allow him to tap or click it to start the rolling process. Here’s what I ended up with:

Notice the panel is far off to the left. It’ll slide in from and out to the left. This is done with a simple animation:

At the end of the animation the x position of the panel is set to 0 which, since it’s anchored in the center of the Canvas moves it to the middle of the screen. I do the exact opposite to slide the panel back off the screen in the animation to close the panel, setting the x position to –1000 at the end of the animation. If you’ve never used animations before, there are a ton of resources to help you learn them. As always, your best first step is the Unity site. Here’s a link to the manual section for animations. Here’s a nice tutorial that covers slide UI controls in and out as I’m doing, as well as a ton of other concepts.

Once I had the animations set up, all that was needed was a small amount of code to do the die rolling:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

 

public class DieManager : MonoBehaviour {

 

    public Sprite[] Dice;

 

    private bool _isRolling;

 

    private float _totalTime;
    private float _intervalTime;

 

    private int _curDie;

 

    Image _die;

 

    private bool _dieRolled;

 


	// Use this for initialization
	void Start ()
    {
        Init();
	}

 

    private void Init()
    {
        _totalTime = 0.0f;
        _intervalTime = 0.0f;
        _curDie = 0;
        _dieRolled = false;
        _die = GameObject.Find("DieImage").GetComponent<Image>();
        _die.sprite = Dice[_curDie];
    }

 

	void Update () 
    {
        if (_isRolling)
        {
            _intervalTime += Time.deltaTime;
            _totalTime += Time.deltaTime;

 

            if (_intervalTime >= 0.1f)
            {
                //change die
                _curDie = Globals.Rnd.Next(0, 6);

 

                //set image to selected die
                _die.sprite = Dice[_curDie];

 

                _intervalTime -= 0.1f;
            }

 

            if (_totalTime >= 2.00f)
            { 
                _isRolling = false;
                _dieRolled = true;
            }
        }

 

	}

 

    public void DieImage_Click()
    {
        if(!_dieRolled)
            _isRolling = true;
    }
}

The Dice member holds the 6 images of the die. It’s public as I set them in the Unity UI:

The script is attached to the Canvas object. The die images are then dragged into place from the folder in the game Assets where I’d placed them.

The _isRolling member is used to determine whether or not the time is tracked between frames in order to know when it’s time to change the die image (every 1/10 of a second) or when the die rolling is completed (after 2 seconds).

The _totalTime and _intervalTime members are added to each frame to know when it’s time to change the die image and when the die rolling is completed.

The _curDie member holds the value of the most recently generated number between 0 and 5 (remember kids, arrays are numbered starting at 0, see the screenshot above!) and is the index into the Dice array for displaying the image.

_die is the reference to the Image control in the Panel.

_dieRolled is used to determine if the animation is done for this turn of the game so the player can’t just keep clicking the image to roll again.

The DieImage_Click event is used in an Event Trigger on the DieImage object:

So how does the panel slide in and out since it doesn’t happen automatically? Two simple events for the buttons in the scene:

    public void PanelTestButton_Click()
    {
        GameObject panel = GameObject.Find("DiePanel");
        Animator animator = panel.GetComponent<Animator>();
        animator.Play("DiePanelOpen");
    }

 

    public void PanelOKButton_Click()
    {
        GameObject panel = GameObject.Find("DiePanel");
        Animator animator = panel.GetComponent<Animator>();
        animator.Play("DiePanelClose");
    }

 

I’ve left a bug in the logic as an exercise for the reader. You can only click the die once. It doesn’t reset when you close the panel. How would you solve this? I’ll post a comment later. 🙂

Creating a Space Shooter Weapon System

So a question came up the other day from Dave Voyles about how to design a weapon system for a space shooter. He needed to figure out how to track what weapons a player had collected and how to switch between them. I got to thinking about it and decided to see what I could come up with. Here’s the basic requirements that I figured would be good to have:

  • A ship could start out with no weapons or 1 or more weapons, with or without ammo
  • A weapon can have infinite ammo or require ammo pickups
  • The player shouldn’t be able to switch to a weapon that hasn’t been equipped on the ship or if it doesn’t have ammo
  • An ammo type could be shared between weapons (think universal energy cells for energy powered weapons)
  • Ammo pickups could have different amounts of ammo for an ammo type
  • Switching between weapons could wrap around both forward or backward. For example, using an Xbox controller, the left and right bumpers could be used to switch to the previous or next weapon – if the first weapon is selected and the left bumper is pressed, the last weapon that’s equipped would be selected.
  • An icon on the game’s HUD would show the currently selected weapon and the ammo available for that weapon would be displayed.

Starting from an empty Unity project, we’ll only need one scene and some GUI elements in that scene. I’m using buttons for all of the player input in this sample, but you would want to handle whatever method of input you’re targeting for your game, touch included. Here’s what my example looks like:

The 5 images on the right are used to display the currently selected weapon, which is the blank image between the Next and Previous buttons. Notice that the button to fire the weapon is initially set to disabled. Whether or not to enable this will be handled by the script we’ll attach to the scene. The small images above the currently selected weapon image will show the weapons that have been equipped on the ship. The buttons to pickup weapons and ammo would be replaced in a real game with sprites or some other element that the player’s ship would interact with.

The next thing we’ll need is a script to control everything and a script to define weapons and ammo. We’ll look at the latter first:

using UnityEngine.UI;

public enum AmmoType
{
    Regular,
    Shotgun,
    Sniper,
    Energy
}

public enum WeaponType
{
    Vulcan,
    SoloGun,
    Sniper,
    ShotGun,
    Seeker
}

public class Weapon
{
    public WeaponType Type;
    public Image HUDIcon;
    public AmmoType Ammo;
    public int AmmoCount;
}

 

For our basic example this is all we’ll need. More than likely your weapon class would have more members and functionality and you might even have an ammo class.

Notice that we only have 4 ammo types. The SoloGun and the Seeker I decided were energy weapons and will share whatever energy ammo is available.

The script to control the interaction with the weapons is only a bit more complex in my opinion:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
using UnityEngine.EventSystems;

public class WeaponInventory : MonoBehaviour
{
    private int[] weaponAmmoRate = new int[] { 10, 1, 1, 1, 5 };

    Dictionary<WeaponType, Weapon> weapons;
    WeaponType curWeaponIndex = WeaponType.Vulcan;

    public Image CurWeaponIcon;

    public Image VulcanImage;
    public Image SoloGunImage;
    public Image SniperImage;
    public Image ShotgunImage;
    public Image SeekerImage;


    public Image MiniVulcanIcon;
    public Image MiniSoloGunIcon;
    public Image MiniSniperIcon;
    public Image MiniShotgunIcon;
    public Image MiniSeekerIcon;


    public Button FireButton;

    public Text AmmoDisplay;

    public bool WrapWeapons = true;

    private Dictionary<AmmoType, int> Ammo; //-1 for infinite


	// Use this for initialization
	void Start () {

        weapons = new Dictionary<WeaponType, Weapon>();

        weapons.Add(WeaponType.Vulcan, new Weapon() { Type = WeaponType.Vulcan, HUDIcon = VulcanImage, Ammo= AmmoType.Regular });

        MiniVulcanIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);

        Ammo = new Dictionary<AmmoType, int>();

        Ammo.Add(AmmoType.Regular, 100);
        Ammo.Add(AmmoType.Energy, 0);
        Ammo.Add(AmmoType.Shotgun, 0);
        Ammo.Add(AmmoType.Sniper, 0);

        FireButton.interactable = true;

        GetCurWeaponImage();
	}

    public void NextWeapon()
    {
        if (!WrapWeapons && (int)curWeaponIndex == Enum.GetValues(typeof(WeaponType)).GetUpperBound(0))
            return;
        else
        {
            WeaponType originalIndex = curWeaponIndex;

            curWeaponIndex++;
            while (!weapons.ContainsKey(curWeaponIndex))
            {
                if ((int)curWeaponIndex > Enum.GetValues(typeof(WeaponType)).GetUpperBound(0))
                    curWeaponIndex = 0;
                else
                { 
                    curWeaponIndex++;
                    if (curWeaponIndex == originalIndex)
                        break;
                }
            }

            GetCurWeaponImage();
            CheckWeapon();
        }
    }

    public void PreviousWeapon()
    {
        if (!WrapWeapons && (int)curWeaponIndex == 0)
            return;
        else
        {
            WeaponType originalIndex = curWeaponIndex;

            curWeaponIndex--;
            while (!weapons.ContainsKey(curWeaponIndex))
            {
                if ((int)curWeaponIndex < 0)
                    curWeaponIndex = (WeaponType)Enum.GetValues(typeof(WeaponType)).GetUpperBound(0);
                else
                {
                    curWeaponIndex--;
                    if (curWeaponIndex == originalIndex)
                        break;
                }
            }

            GetCurWeaponImage();
            CheckWeapon();
        }
    }

    private void CheckWeapon()
    {
        bool enable = Ammo[weapons[curWeaponIndex].Ammo] > 0;

        FireButton.interactable = enable;
    }

    private void GetCurWeaponImage()
    {
        CurWeaponIcon.sprite = weapons[curWeaponIndex].HUDIcon.sprite;

        UpdateAmmoDisplay();
    }

    public void PickupVulcanWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Vulcan))
        { 
            weapons.Add(WeaponType.Vulcan, new Weapon() { Type = WeaponType.Vulcan, HUDIcon = VulcanImage, Ammo = AmmoType.Regular });
            MiniVulcanIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSoloGunWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.SoloGun))
        {
            weapons.Add(WeaponType.SoloGun, new Weapon() { Type = WeaponType.SoloGun, HUDIcon = SoloGunImage, Ammo = AmmoType.Energy });
            MiniSoloGunIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSniperWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Sniper))
        { 
            weapons.Add(WeaponType.Sniper, new Weapon() { Type = WeaponType.Sniper, HUDIcon = SniperImage, Ammo = AmmoType.Sniper });
            MiniSniperIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupShotgunWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.ShotGun))
        { 
            weapons.Add(WeaponType.ShotGun, new Weapon() { Type = WeaponType.ShotGun, HUDIcon = ShotgunImage, Ammo = AmmoType.Shotgun });
            MiniShotgunIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSeekerWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Seeker))
        { 
            weapons.Add(WeaponType.Seeker, new Weapon() { Type = WeaponType.Seeker, HUDIcon = SeekerImage, Ammo = AmmoType.Energy });
            MiniSeekerIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }


    public void FireWeaponButtonClick()
    {
        Ammo[weapons[curWeaponIndex].Ammo] -= weaponAmmoRate[Convert.ToInt32(weapons[curWeaponIndex].Ammo)];

        if (Ammo[weapons[curWeaponIndex].Ammo] == 0)
            FireButton.interactable = false;

        UpdateAmmoDisplay();
    }

    private void UpdateAmmoDisplay()
    {
        AmmoDisplay.text = Ammo[weapons[curWeaponIndex].Ammo].ToString();
    }

    public void PickupRegularAmmoButtonClick()
    {
        Ammo[AmmoType.Regular] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupEnergyAmmoButtonClick()
    {
        Ammo[AmmoType.Energy] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupShotgunAmmoButtonClick()
    {
        Ammo[AmmoType.Shotgun] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupSniperAmmoButtonClick()
    {
        Ammo[AmmoType.Sniper] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

}

I had originally planned on using an enum for weaponAmmoRate, but the duplicate values made that impractical. Obviously the values need to match the order of the WeaponType enum.

The weapons variable holds all the possible weapons a ship can carry, even if they’re not equipped. If you add new weapons to the WeaponType enum, there will automatically be room for them in the possible weapons the ship can equip, no change necessary.

The curWeaponIndex is straightforward, it points to the item in the weapons array that’s currently selected.

CurWeaponIcon is the image in the scene that shows the player the weapon that’s currently selected. We need the member here to allow us to change it when the player uses an input device to switch between weapons. We could grab the Image every time the weapon is switched, but that’s cumbersome, more code than we need to use and wastes time. You’ll see this a lot in Unity, having a member in a script that is hooked to a UI object. This is one of the features of Unity that I really like. The Unity IDE recognizes the public members of a script and allows you to set them by dragging something from the Hierarchy onto it in the Inspector.

The next two chunks of Images are used to set the CurWeaponIcon and display which weapons the ship has equipped respectively.

We have a reference to the button that fires the selected weapon as we need to enable or disable it when the selected weapon is changed or if the selected weapon runs out of ammo.

As the player fires the selected weapon, we use the AmmoDisplay member to update the amount of ammo that’s available for the weapon.

Some games allow the selection of the weapon to wrap from last to first or vice versa when navigating through the equipped weapons. The WrapWeapons member allows us to decide whether or not we enable this feature. The code for handling the navigation between weapons uses this member to correctly set the selected weapon as we’ll see shortly.

The Ammo member holds the amount of ammo for every type of weapon. In the case of the some weapons you might want this is be infinite. This is usually the case with the default or first weapon that’s equipped in the ship. Setting the value to –1 for a weapon allows for infinite ammo for a weapon.

At the start of the scene we create our weapons array, add a Vulcan to it, set all the UI elements to indicate we have a weapon set, and add some ammo for it.

In the NextWeapon and PreviousWeapon methods we first check to see if the current weapon is the last or first respectively and if weapon wrap is not turned on we just return as there’s nothing to do. We then go through the weapons list checking to see if a weapon has been equipped on the ship to select. If we go through the entire list and don’t find one we just re-select the original weapon. We set the select weapon icon and check to see if the weapon has ammo, disabling the fire button if not. We also update the display of the available ammo for the selected weapon.

For each of the method for picking up a weapon we first see if the weapon is already equipped. If not, we create an instance in the weapons list and set the icon to indicate that weapon is available.

When the fire button is pressed we subtract from the ammo for that weapon based on the weapon rate for the weapon and disable the weapon is the ammo has run out. There’s a potential bug in this code. See if you can find it. 😉

When ammo is picked up, we grab the Tag property of the GameObject to see how much ammo it contains and add that to the proper element in the Ammo array. One thing that you could change for a real game is add a property to the ammo GameObject to indicate what type of ammo it is and have one method for handling all ammo pickups. Try replacing the buttons with a GameObject that does this.

That’s it. Pretty simple I think, but it should be enough to at least start you out with a space shooter type game. As also, feedback and questions are welcome. 🙂