- Home /
Problem with my weapons switch statement C#
Found the answer. Just have to make the gun stop shooting when I press the button.
void OnGUI()
{
if(GUI.Button(new Rect(250, 10, 50, 30), "SemiAuto22"))
{
gunType = GunType.SemiAuto22;
Guns ();
}
}
My enum GunType determines the stats of the gun. Some of the stats have no meaning at the moment. Right now the only stats that affect anything are
rpm(rounds per minute),
maxMagSize(the maximum rounds the gun can have),
magSize(the current rounds of the gun),
and secondsBetweenShots.
The problem im having is exactly where to put my method Guns(). The only issue im noticing at the moment is that if I put Guns() in the start method, the GunTypes variables wont change if I try to change my weapon in runtime. If I try to put it in my Update method, the GunTypes ammo will constantly refill. And if I put it in my Shoot() method everytime I pull the trigger the ammo refills.
Im sure there is a way to get this to work I just cant figure it out.
This is where I declare my enum and its variables before the start method
public enum GunType
{
Revolver22 = 0,
SemiAuto22 = 1,
SemiAuto9mil = 2
};
public GunType gunType = GunType.Revolver22;
public float rpm;
public float maxMagSize;
public float magSize;
public float damage;
public float cost;
My guns method
void Guns()
{
switch(gunType)
{
case GunType.Revolver22:
rpm = 90;
maxMagSize = 8;
magSize = 8;
damage = 3;
cost = 0;
secondsBetweenShots = 60/rpm;
break;
case GunType.SemiAuto22:
rpm = 120;
maxMagSize = 12;
magSize = 12;
damage = 3;
cost = 25;
secondsBetweenShots = 60/rpm;
break;
case GunType.SemiAuto9mil:
rpm = 120;
maxMagSize = 12;
magSize = 10;
damage = 5;
cost = 75;
secondsBetweenShots = 60/rpm;
break;
}
}
How my shooting works
public void Shoot()
{
if(CanShoot())
{
Ray ray = new Ray(spawn.position,spawn.forward);
RaycastHit hit;
float shotDistance = 200;
if (Physics.Raycast(ray,out hit,shotDistance))
{
shotDistance = hit.distance;
if(hit.collider.gameObject.tag == "Enemy")
{
///Something Needs to happen here.
hit.collider.gameObject.SendMessage("DamageEnemy", 2);
Debug.Log("You Hit an Enemy");
}
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
audio.Play();
if (tracer)
{
StartCoroutine("RenderTracer", ray.direction * shotDistance);
}
Rigidbody newShell = Instantiate(shell,chamber.position,Quaternion.identity) as Rigidbody;
newShell.AddForce(chamber.forward * Random.Range (150f,200f) + spawn.forward * Random.Range(-10f,10f));
magSize -=1;
}
}
private bool CanShoot()
{
bool canShoot = false;
if (magSize > 0)
{
canShoot = true;
if(Time.time < nextPossibleShootTime)
{
canShoot = false;
}
}
else
{
Reload();
}
return canShoot;
}
private void Reload()
{
magSize = maxMagSize;
}
Answer by Airmand1 · Jul 31, 2014 at 05:20 AM
Doing this worked perfectly. Just have to make the gun not shoot when pressing the button.
void OnGUI()
{
if(GUI.Button(new Rect(250, 10, 50, 30), "SemiAuto22"))
{
gunType = GunType.SemiAuto22;
Guns ();
}
}
Answer by Kiwasi · Jul 31, 2014 at 04:24 AM
Guns should be inside the update method, inside an if statement that checks if the user has pressed the change gun key.
Guns should also be called ChangeGun. Make your method names specific, you will regret not doing so later.
Thank you for the response. However there is no change gun button persay. This is a top down defense type game. Not an FPS. You would buy a new gun through clicking a button on the screen. And unless Im mistaken buttons have to be in an OnGUI() function.
Okay, now I have no clue what the question is.
If it needs to be set once then run it in Awake (best for internal stuff) or Start (best for stuff that relies on other GameObjects) or OnEnable (best for object pooling)
Or you could run it in the set method for gunType. Or put it on your button.
You really don't have enough information for a meaningful answer.
I actually just figured it out before you posted that comment. Essentially yes its just putting it on that button.
Answer by Arcadewiz · Jul 31, 2014 at 05:01 AM
According to the logic you are trying to accomplish, I don't think the magSize should be placed in the cases of the switch-case.
The magSize variable I gather will change when the player shoots and your switch case is meant to be only for the purpose of switching between weapon types. So it is better that you remove that from the switch-case part. Your logic construction is not accurate. When the switch-case initiates your weapons will also take in the magSize value that you have determined in the cases. I think you should either considering a struct for weapon types or object classes so as to selectively call on variables dynamically. Then you can make the magSize variable either static according to your needs or simply use the switch case for switching between weapons. Hope this helps..