I’m very new to C sharp in unity, At the moment I’m making and R type game. My spaceship has four settings for its gun.
1)Single shot mode, fires one bullets
2) Double shot mode, fires two bullets
3) Blast mode, fires four bullets
4) Machine gun mode, fires eight bullets
The problem I have if the player has seven bullets and uses machine gun mode, he will fire eight bullets the ammo counter will display -1.
What I want to happen if the player doesn’t have enough bullets for the mode, he will not be able to use that mode, and also the ammo counter doesn’t display negative numbers.
I have written some c# code which does the job, but feels a bit brute force. I was wondering is there a more concise way of writing it.
C# code below
public float ammo1 = 1; //Singleshot Mode
public float ammo2 = 2; //Double shot Mode
public float ammo3 = 4; //Blast mode
public float ammo4 = 8; //Machine gun mode
public float ammoMax = 10;
public bool onOff1; // ON Off for Singleshot Mode
public bool onOff2; // ON Off for Double shot Mode
public bool onOff3; // ON Off for Blast mode
public bool onOff4; // ON Off for Machine gun mode
public Text AmmoDis;
// Use this for initialization
void Start () {
}
/// Update is called once per frame
void Update () {
Debug.Log ( "ammoMax " + ammoMax);
//Singleshot Mode
if(!onOff1 && Input.GetKeyDown(KeyCode.H)){
ammoMax -= ammo1;
}
if (!onOff1 && ammoMax == 0){
onOff1 = true;
Debug.Log ( "it wook ");
}
// Double shot Mode
if(!onOff2 && Input.GetKeyDown(KeyCode.J)){
ammoMax -= ammo2;
}
if (!onOff2 && ammoMax == 1){
onOff2 = true;
Debug.Log ( "it wook2 ");
}
if (!onOff2 && ammoMax == 0){
onOff2 = true;
Debug.Log ( "it wook2 ");
}
//Blast mode
if(!onOff3 && Input.GetKeyDown(KeyCode.K)){
ammoMax -= ammo3;
}
if (!onOff3 && ammoMax == 1){
onOff3 = true;
Debug.Log ( "it wook3 ");
}
if (!onOff3 && ammoMax == 2){
onOff3 = true;
Debug.Log ( "it wook3 ");
}
if (!onOff3 && ammoMax == 3 ){
onOff3 = true;
Debug.Log ( "it wook3 ");
}
if (!onOff3 && ammoMax == 0){
onOff3 = true;
Debug.Log ( "it wook3 ");
}
//Machine gun mode
if(!onOff4 && Input.GetKeyDown(KeyCode.L)){
ammoMax -= ammo4;
}
if (!onOff4 && ammoMax == 1){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 2){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 3 ){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 4 ){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 5 ){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 6 ){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 7 ){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
if (!onOff4 && ammoMax == 0){
onOff4 = true;
Debug.Log ( "it wook4 ");
}
AmmoDis.text = ammoMax.ToString();
}
}
As far as making your code more concise, you can make a class which holds the data for each type of weapon, and then use an array of these classes to represent all of your weapons.
[System.Serializable] //this line makes it editable in the inspector
class Weapon {
public string name = "Default";
public KeyCode trigger;
public int ammoPerShot = 1;
public int currentAmmo = 0;
public bool CanBeShot() {
return (currentAmmo >= ammoPerShot);
}
public void UseAmmo() {
currentAmmo = Mathf.Max(currentAmmo - ammoPerShot, 0);
}
}
public Weapon[] allWeapons;
void Update() {
for (int w=0;w<allWeapons.Length;w++) {
if (Input.GetKeyDown(allWeapons[w].trigger) && allWeapons[w].CanBeShot() ) {
Debug.Log("Shot the weapon "+w);
allWeapons[w].UseAmmo();
}
}
In the inspector you can add weapons to your heart’s content, picking their keycodes and renaming them. And you can add new weapons to this without needing to change a bunch of code! There are a number of ways you can handle the weapon firing logic itself, including a GameObject variable in the class (which just gets instantiated), or a UnityEvent, to which you can assign a function in the main script (if you need additionally unique logic per weapon), or you can simply do some logic based on the weapon’s name - feel free to ask for help on implementing any of these.
Thank you for your help You are right I didn’t test if I had enough bullets before substarcting them from the ammo. I don’t know how I missed that, with the project I could not see the wood from the trees.
I don’t know if I’m missing something, but the your code does seem not work properly.
You have converted my bool into int.
At the start of the game gunState int = 0,
Single shot mode will not work because if gunState = 0, then gunState is not equal to 0 the if statement will not complete.
The other gun modes work once
Hi Griff, there are some good answers to your question here, but I just have one question if you fire your machine gun and you only have 7 bullets left, do you want it to not fire at all (which StarMana’s code will do perfectly) or to fire the 7 bullets that are left?
My problem was I placed “playerAmmoMaxFloat -= playerAmmoDamage1” in the if statement not “void Fire4()” . Which was a creating a weird loop which gave me minus numbers.