- Home /
Storing weapon types and changing between them
My game has 4 weapons that the player can use. Each have different properties. They are triggered by getting a powerup and are as follows:
Tomato - default and fall back weapon
Pumpkin - get 10 by default. Once run out, revert to previous weapon.
Watermelon - Like a bomb. You get 1 and then after using revert to previous weapon.
Rapid Fire - Machine gun style weapon using the tomato. This lasts for 5 seconds.
During gameplay, they all work as I need them to and I have omitted the firing function form the code since its quite long and deals with touches. It works 100% though. The problem is that once a new powerup is attained, it replaces the previous one. So if you have a pumpkin, then get a watermelon, once you use the watermelon, it reverts back to the default tomato weapon. What should happen is that it stores the weapons in a list and when you get a new one, it becomes the current weapon, and the existing one becomes the previous one that it will revert to once the current one has finished. So if you have Pumpkins then get the watermelon or rapid fire, once they run out, it reverts back to pumpkins then once they run out it reverts back to the default tomato.
I could get this working with only 3 weapons, but once I added the rapid fire, it stopped working as desired. I could only get the rapid fire powerup if pumpkins were the previous weapon.
The problem I suspect is that the update is always checking if the rapid fire duration has finished or that the pumpkin count is less that 0, and thats where it won't allow one or the other.
What I think needs to happen is when you get a weapon it checks to see if it is the same as the current one, and if it isnt, pushes the name to the previous weapon it can they use once the current one has expired. It would need to store up to 3 previous weapon names in case the player gets all weapon powerups in quick succession (highly unlikely but I'm covering all bases.
Heres what I have:
Any help greatly appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WeaponManager : MonoBehaviour {
public GameObject weaponIcon; //Our Powerup/Projectile Icon at the top of the screen
public UILabel weaponLabel; //Our label to hold weapon text
public string currentWeapon;
public string previousWeapon;
public string previousWeapon2;
public string previousWeapon3;
public int pumpkinProjectileCount;
public int pumpkinIncrease = 10; //the amount of pumpkins we add when we run the function
public bool hasWatermelon = false; //if we have the watermelon
public float rapidFireDuration = 5.0f; //duration for our straight shooter
void Start ()
{
currentWeapon = "Tomato";
}
void Update ()
{
#region key Inputs
//shortcut keys to run the functions
if(Input.GetKeyDown("w"))
{
Watermelons();
}
if(Input.GetKeyDown("p"))
{
Pumpkins();
}
if(Input.GetKeyDown("m"))
{
RapidFire(5.0f);
}
if(Input.GetKeyDown("d"))
{
pumpkinProjectileCount -=1;
}
#endregion
#region icons switch
//used for displaying the NGUI spites for each weapon
//and decrementing the rapid fire duration
switch(currentWeapon)
{
case "Pumpkin":
weaponIcon.GetComponent<UISprite>().spriteName = "Pumpkin";
weaponLabel.text = pumpkinProjectileCount.ToString();
break;
case "Watermelon":
weaponIcon.GetComponent<UISprite>().spriteName = "Watermelon";
weaponLabel.text = "";
break;
case "RapidFire":
weaponIcon.GetComponent<UISprite>().spriteName = "RapidFire";
weaponLabel.text = rapidFireDuration.ToString("##");
rapidFireDuration -= Time.deltaTime;
break;
case "Tomato":
weaponLabel.text = "";
weaponIcon.GetComponent<UISprite>().spriteName = "Tomato";
break;
}
#endregion
//getting lost in this section......
//I think it is only ever checking the pumpkins or the watermelons
if(pumpkinProjectileCount <= 0 && hasWatermelon == false)
{
if(previousWeapon == "RapidFire")
{
currentWeapon = "RapidFire";
}
else if(previousWeapon == "Watermelon")
{
currentWeapon = "Watermelon";
}
else
{
currentWeapon = "Tomato";
}
}
if(rapidFireDuration <= 0.0f) //if our rapid fire duration has expired
{
//tried using a switch case but it would only store the previous weapon
//and not check if they were the same
switch(previousWeapon)
{
case "Pumpkin":
previousWeapon = "Pumpkin";
break;
case "Watermelon":
previousWeapon = "Watermelon";
break;
case "RapidFire":
previousWeapon = "RapidFire";
break;
case "Tomato":
weaponLabel.text = "";
}
}
}
#region change weapons
void Pumpkins()
{
previousWeapon3 = previousWeapon2;
previousWeapon2 = previousWeapon;
previousWeapon = currentWeapon;
if(currentWeapon == "Pumpkin")
{
pumpkinProjectileCount += pumpkinIncrease;
}
else
{
previousWeapon = currentWeapon;
currentWeapon = "Pumpkin"; //Change our projectile type to pumpkins
pumpkinProjectileCount = pumpkinIncrease;
}
}
void Watermelons()
{
hasWatermelon = true;
previousWeapon = currentWeapon;
currentWeapon = "Watermelon"; //Change our projectile type to watermelons
}
void RapidFire(float duration)
{
previousWeapon3 = previousWeapon2;
previousWeapon2 = previousWeapon;
previousWeapon = currentWeapon;
if(currentWeapon == "RapidFire")
{
rapidFireDuration += duration;
}
else
{
previousWeapon = currentWeapon;
currentWeapon = "RapidFire"; //Change our projectile type to rapid fire
rapidFireDuration = duration;
}
}
#endregion
void OnGUI()
{
GUI.Box(new Rect(20,300,200,40),"current:" + currentWeapon);
GUI.Box(new Rect(20,350,200,40),"previous:" + previousWeapon);
GUI.Box(new Rect(20,400,200,40),"previous2:" + previousWeapon2);
GUI.Box(new Rect(20,450,200,40),"previous3:" + previousWeapon3);
}
}
Thanks again!
Answer by Ermarrero · Apr 05, 2014 at 05:30 AM
Why dont you use booleans like switches? It will make your life coding this a lot easier.