- Home /
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!
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.
hmmm ok , thanks for your help , if anyone else has anything to add please do!
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
I don't think it matters which way you do it. Just keep in $$anonymous$$d there is a slight performance penalty for creating and destroying objects, but that should not make any difference unless you're optimizing for mobile.
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
Your answer
Follow this Question
Related Questions
How to prevent a object (attached to the main camera) sticking into a wall? 0 Answers
Destruction Help!!!!! 1 Answer
How to switch weapons?(c#?) 4 Answers
How do I add a weapon/object to my character? 1 Answer
How to get zombie AI working 3 Answers