Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SNOWRAPH · Nov 19, 2016 at 07:03 AM · 3d3d models1st person3rd person controller

Weapon switching (a lot)

I have a huge number of weapon in my game and I want the most efficient way to switch between them. I know you can disable and enable weapon but I don't think its the most efficient way.

Thanks

Sorry for my english (I'm french)

Comment
Add comment · Show 2
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 roman_sedition · Nov 19, 2016 at 09:56 AM 0
Share

How many is a lot of weapons?

Even though being able to potentially cycle through 20 or so weapons on command would be useful, if it's a fast action game requiring a keyboard it is usually most effective to just allow the player to map their 5 most favored weapons to hotkeys. The limitation of hotkeys serves as a sense of balance and gives the player the responsibility of making decisions.

If this is a slower paced game which still uses a keyboard, I'd recommend a shell like system for example the ' (apostrophe key) opens up a command input, you type in the name (or the shortcut of the name) and press enter and then it switches weapons accordingly.

e.g

 'swd9

Opens up the prompt, changes to equip sword 9 (swd9 is a shortcut that the user can customize themselves)

If this is a console game, I liked how in Turok 2 (I think), you hold a button and it shows a UI, you then press the analog stick in the direction of the weapon you want to switch to and then release the held button to switch to it.

avatar image SNOWRAPH · Nov 19, 2016 at 04:00 PM 0
Share

I want to be able to add weapon later in développement so I expect there will be a lot of weapon at the end. Thanks

1 Reply

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

Answer by Dibbie · Nov 19, 2016 at 08:38 AM

The easiest and most efficient way I can think of, is to create a weapon switching script on the player, with 2 public variables: 1. for the current weapon they are using 2. for the array of all their possible weapons

You can then code it however youd like to switch weapons with the scroll wheel or input keys, and change the current weapon, to the next element of your array (depending on the direction it should be searching in the array - if they want to switch to a previous weapon for example, it would search 1 UP(--) the array, and next weapon would search 1 DOWN(++) the array)

Generally, that could look something like (C#, all untested code):

 //global variables
 public GameObject currentWeapon;
 public Transform playersHands; //you dont technically NEED this, if you dont have a lot going on in your game, to do a GameObject.Find(), otherwise, it would be best to know exactly where to place the weapon
 public GameObject[] WeaponInventory;
 
 private int index = 0; //this will keep track of where you are in your array, when switching weapons
 
 //in Start
 currentWeapon = WeaponInventory[0]; //the first index of the array is the weapon they will start with - change "0" to a different number if you want them to start with a different weapon
 
 //in Update
 
 if(Input.GetKeyDown(KeyCode.Q){
 //switch to the NEXT weapon
 if(index + 1 > WeaponInventory.Lenth){ index = 0; } else { index++;} //keep index, within the bounds of the array -- if they are on the last weapon of the array, loop back to the first weapon
 
 currentWeapon = WeaponInventory[i]; //switch the weapon
 if(hands.childCount > 0){ //if the player HAS a weapon currently out
 Destroy(hands.GetChild(0)); //destroy their current weapon
 }
 GameObject weapon = (GameObject) Instantiate(currentWeapon,hands.position, hands.rotation); //create their selected weapon at their hands
 weapon.transform.parent = hands; //parent the weapon to their hands
 //at this point here, is where you can play the weapons "withdraw" or "switching" animation if you have one.
 }

You could even go so far to say you dont even need "currentWeapon", and use the array at the index in the Instantiate.

Comment
Add comment · Show 1 · 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 SNOWRAPH · Nov 19, 2016 at 04:01 PM 0
Share

Thanks, very useful anwser

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

I need help with a model 0 Answers

Players moves with simple script, but dont look where he moves 2 Answers

Objects rendering improperly 1 Answer

Blender import bug 0 Answers

Does unity3d support the .ply file(maybe a 3D data formatter) in android app? 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