Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Airmand1 · Jul 31, 2014 at 04:10 AM · c#switchenumswitch-case

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;
     }








Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 ();
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Airmand1 · Jul 31, 2014 at 04:38 AM 0
Share

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.

avatar image Kiwasi · Jul 31, 2014 at 04:43 AM 0
Share

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.

avatar image Airmand1 · Jul 31, 2014 at 04:52 AM 0
Share

I actually just figured it out before you posted that comment. Essentially yes its just putting it on that button.

avatar image
0

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..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why would my switch's case be correct but the output is not? 2 Answers

Why does my Switch statement only go through one case correctly? 1 Answer

Changing enum values 1 Answer

I would like some help refactoring my code, I am working with switch cases concering the delivery of food items in my game 1 Answer

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges