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 jiri-vyc · Nov 08, 2014 at 10:50 AM · weaponweaponsswitching

Weapon switching

Hello everyone, I am facing a little problem with weapon switching in my game. How to do it properly?

First of all, my game is local multiplayer shooter. That means, I have several players in a scene and several weapons available to pick up/being held by each player. I am planning to have very big variety of weapons and considerable amount of players. That is why I think following solution is a very bad way how to do it:

Spawn each player with every weapon in the game, current weapon as active=true and possibly owned weapons as owned=true/false. Something like this in a script:

 public GameObject[] weapons;


 //somewhere in the code, "num" being currently owned weapon which I want to switch to
 public void ChangeWeapon(int num)
         {
             for(int i = 0; i < weapons.Length; i++)
                {
                 if(i == num)
                   weapons[i].gameObject.SetActive(true);
                 else
                   weapons[i].gameObject.SetActive(false);
                }
         }
 

And in the scene, drag every possible weapon to the public weapons array.

This is imho very bad way of doing this, because that means lots and lots of unnecessary objects will be in the scene (all players + all possible weapons for each of them). Performance will take a big hit I think.

Any ideas how to do this in more efficient way?

EDIT: I was thinking about loading(and instantiating) a prefab of desired weapon each time and setting it's position etc to the current weapon's position, setting parent to player object and then deleting the old weapon. Problem is, I seem to not be able to successfully do that. I am not able to instantiate the new weapon properly and give it all the parameters I need in the Awake() function - I am setting for example id of player(owner) like this:

 GetComponentInParent<Player>().playerNumber

And even if - I'm not sure if this is the right solution performance-wise.

Comment
Add comment · Show 4
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 Linus · Nov 08, 2014 at 10:56 AM 0
Share

Worry about performance when/if you need to worry about performance. I used to play around with FPS Constructor that package also abased around having one of each possible weapon assigned in a list. By the way its free now, so might learn a few tricks from it.

To the right on this page there is a related Questions box, I bet a solution can be found there as well.

avatar image richyrich · Nov 08, 2014 at 11:33 AM 0
Share

Giving every player every weapon is poor choice for gameplay since it does not encourage players to think strategically about the best weapon for them, the map, etc. It would be better (imho) to limit the number of each weapon type so that there are not enough to go around all players. As for the weapons themselves, you could create an array of weapons which can then be used when found/picked up/selected. The array means that GameObjects are not continuously being destroyed/instanced and will save you processor time.

With regards to your loop, you're right it's not good. A better choice would be to remember the index of the last weapon used. The function for change is then simple with no loop: disable the weapon of the old index, enable weapon of new index, update old index to equal new index.

With regards to the above though, in general looking for performance improvement gains at the beginning of game development is not going to help you much. You should just build a prototype game that runs with an ok Frame Per Second - improving only when further features become impossible to add because your existing code is so slow. Once you have your basic prototype, then you look back and reassess what you know, then rebuild with a proper architecture. Don't be afraid to have a code base littered with comments and hacks - just write little notes to self as you go (and then sort it all out with the proper design later)

avatar image jiri-vyc · Nov 08, 2014 at 07:25 PM -1
Share

in general looking for performance improvement gains at the beginning of game development is not going to help you much

This actually goes agains my whole process of doing things, but I will definitely try to switch to that with this game. It's just I find very difficult to re-factor my code after it's somehow finished and to find out new, better way if I already did it differently before.

But anyway, generaly, theoretically speaking, is my assumption right or completely wrong? Is performace affected more by, let's say

  1. 5 different players, 15 different weapons, each weapon gameObject is present in a scene, just as active=false. That makes 75 objects just for the weapons. $$anonymous$$s to many Objects in the game (memory issues?).

  2. 5 different players, 15 different weapons, instantiating/loading another weapon mesh+behaviour every time player switches weapon, or finds weapon on a ground. $$anonymous$$s to less objects, but more computational power I guess (CPU issues?).

Now I'm thinking about it, I'm not fully convinced that 2 is overall better for performance anyway.

avatar image richyrich · Nov 08, 2014 at 08:24 PM 0
Share

@jiri.vyc $$anonymous$$y paragraph two will help with your original questions of removing the loop. A bank of weapons which can be reused will help performance as in paragraph one.

As for your other questions, they're more suited to the forums as they involve discussion about what else is going on in your game which deter$$anonymous$$es whether or not something is going to affect your game, whether it's worth doing something etc.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by amiel_ace · Nov 08, 2014 at 06:10 PM

You don't actually have to use separate objects for different weapons. Why not try making an empty game object for the pick-ups and another one for the real weapons equipped. Then this way all you need to do is change their mesh and some behaviors, So you only got two objects.

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 richyrich · Nov 08, 2014 at 06:54 PM 0
Share

The OP's original statement included:

weapons available to pick up/being held by each player

...Therefore the separate objects already exist (in order to be visible in the field). I'm not sure how dynamic modification of additional two objects per player will assist?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon Switch System for Gear VR Controller 0 Answers

Weapon Switching 2 Answers

Switching weapons animation with mecanim? 2 Answers

GUI display weapon name you are looking at 1 Answer

Weapon shooting mechanism problem. 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