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 HumanError · Dec 17, 2012 at 08:13 PM · weaponefficiencyswitching

Making Weapon Switching Code more effective.

Hello. I made a simple weapon switching code for my character.

I want to ask, there is a way to make this code more effective?

This is my code:

 if (Input.GetKeyDown ("1"))
    {
       weaponID = 1;
    }
    
    if (Input.GetKeyDown ("2"))
    {
       weaponID = 2;
    }
    
    if (Input.GetKeyDown ("3"))
    {
       weaponID = 3;
    }

I am using JS.

Comment
Add comment · Show 1
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 Dexter.Unity · Dec 17, 2012 at 08:22 PM 0
Share

You want to make it more effective how? Faster compiling of the code ? Or do you want a for loop since the weapon is linked to the key directly?

3 Replies

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

Answer by wannabee · Dec 17, 2012 at 08:49 PM

You could do something like:

 function changeWeapon() {
         for (var i : int = 1; i<=3; i++) {
             if(Input.GetKeyDown(i.ToString())) {
                  weaponID = i;
                  return;
             }
         }
 }

If you would like...

It won't be more efficient in the sense that it won't run faster, but I do like this coding style better, even though it might hurt readability a bit. Also, now it's easy to add more weapons, just change that number 3... :)

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 MirekPluta · Dec 17, 2012 at 08:49 PM

What exactly do you mean by "more effective" ? If you mean performance, then you'll get little improvement when instead of 3 separate if statements you use if-elseif-elseif statement.

 if (Input.GetKeyDown ("1"))
    {
       weaponID = 1;
    }
    else if (Input.GetKeyDown ("2"))
    {
       weaponID = 2;
    }
    else if (Input.GetKeyDown ("3"))
    {
       weaponID = 3;
    }

You could also create some dictionary containing key-weapon pairs and iterate it to check for weapon switch.

     var key_weapon = { "1" : 1, "2" : 2, "3" : 3};
 
     for( kvp in key_weapon)
     {
         if(Input.GetKeyDown(kvp.Key.ToString()))
         {
             weaponId = kvp.Value;
         }
     }
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 Karsnen_2 · Dec 17, 2012 at 08:35 PM

I do not think there is much you could do with what you already have. While developing, optimizing could also be a devil. So try to be effective or optimize your code only when your target platform only demands. Else just forget it.

READABLE CODE :

The best way that you could do, by which I mean to say is make it clean to read is -> Create a method that would receive int as the parameter. Use this int in a switch case to change weapon. On your update method call the method by passing the appropriate parameter.

CODE : It is in C#.

     void Update ()
     {
         if (Input.GetKeyDown ("1"))
                ChangeWeapon(1);
 
        if (Input.GetKeyDown ("2"))
             ChangeWeapon(2);
 
            if (Input.GetKeyDown ("3"))
              ChangeWeapon(3);
     }
     
     
     void ChangeWeapon (int i)
     {
     
         switch (i)
         {
         case 1 :
             // Switch to the required weapon
             break;
         case 2:
             // Switch to the required weapon
             break;
         case 3:
             // Switch to the required weapon
             break;
         default:
             break;
             
         }
     }
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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Does Weapon Selection Work In-Game? 0 Answers

Weapon Switching 2 Answers

WeaponScript not working 0 Answers

Weapon Switching 2 Answers

Weapon Switch System for Gear VR Controller 0 Answers


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