• 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 jimmycrazyskills · Feb 14, 2014 at 12:55 AM · playerweaponhow toswap

How to swap weapons easily?

Hi,what would be the best way of swapping weapons on a player? , for example I could have every weapon already a child of the player and only render one at a time , but I'm guessing this is extremely inefficient. I have attached a picture(epic paint skills ;) ) to try and show what I mean , sorry if this question isn't specific enough! alt text

question picture.png (31.4 kB)
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 getyour411 · Feb 14, 2014 at 12:57 AM 1
Share

Your first approach is oft used. Another approach is to create a "hook" empty gameObject on the hand and based on player Input/actions to parent a new weapon to that and remove the other.

avatar image jimmycrazyskills · Feb 14, 2014 at 01:06 AM 0
Share

hmmm ok , thanks for your help , if anyone else has anything to add please do!

avatar image KrisJulio · Feb 14, 2014 at 01:59 AM 0
Share

Yes The first comment is good. But ins$$anonymous$$d of removing the gameObject of the Gun. You can just show and hide. Just position all the possible weapon. And show whenver the user want's it to show

avatar image AdamScura · Feb 14, 2014 at 02:18 AM 0
Share

I don't think it matters which way you do it. Just keep in mind there is a slight performance penalty for creating and destroying objects, but that should not make any difference unless you're optimizing for mobile.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by 4u2fap · Feb 14, 2014 at 04:44 AM

well if you have all the weapon as a child of the camera (assuming you are making an fps) attach a script to the camera. then you can create a list of GameObjects for your gun that is attach and then enable or disable them.

for example:

 var gun1 : GameObject;
 var gun2 : GameObject;
 
 function Start() {
 gun1.gameObject.SetActive(true);
 gun2.gameObject.SetActive(false);
 }
 
 function Update(){
     if (Input.GetKeyDown("1")){
     gun1.gameObject.SetActive(true);
     gun2.gameObject.SetActive(false);
     }
     else if (Input.GetKeyDown("2")){
     gun1.gameObject.SetActive(false);
     gun2.gameObject.SetActive(true);
     }
 }

this scrip is not the best way to do it and has not been tested so they may be some issues with it but i think you get the point of what im saying.Dont forget to add your inputs 1 and 2 by going into edit -project settings - Input

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to swap purchased weapons over Photon Network? 1 Answer

I am making a Call of Duty type game, how can I make the people settings just like call of duty? 2 Answers

Player Swap 1 Answer

Why is my enemy stopping before it reaches the player? 1 Answer

Weapon switching problem 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges