- Home /
gun manager how to
does anyone know how to make a gun manager script in javascript? if u pick up a gun it adds it to an inventory or something a little like that.i guess u have to add a new input but i dont know where to go from there. i need the gun to be part of my character when he picks it up(i have my own character)
thank you
Answer by Jason_DB · Feb 15, 2011 at 08:09 PM
Basically you just need an array to store the weapons in, from which you can select them. I do it this way (although it's not the prettiest, it allows 1-10 weapons). Basically, if you hit a number key 1-0 it selects a weapon from the array. All you would need to do is have your guns on the character but turned off (invisible/not able to fire) and have functions on the gun to turn them on or off, then if you put it in the weapons array it will be selectable.
var weapons : GameObject[]; var selectedWeapon : int; function Update () { if (Input.GetKeyDown("1") && weapons.length >= 1) { SelectWeapon(0); selectedWeapon = 0; } else if (Input.GetKeyDown("2") && weapons.length >= 2) { SelectWeapon(1); selectedWeapon = 1; } else if (Input.GetKeyDown("3") && weapons.length >= 3) { SelectWeapon(2); selectedWeapon = 2; } else if (Input.GetKeyDown("4") && weapons.length >= 4) { SelectWeapon(3); selectedWeapon = 3; } else if (Input.GetKeyDown("5") && weapons.length >= 5) { SelectWeapon(4); selectedWeapon = 4; } else if (Input.GetKeyDown("6") && weapons.length >= 6) { SelectWeapon(5); selectedWeapon = 5; } else if (Input.GetKeyDown("7") && weapons.length >= 7) { SelectWeapon(6); selectedWeapon = 6; } else if (Input.GetKeyDown("8") && weapons.length >= 8) { SelectWeapon(7); selectedWeapon = 7; } else if (Input.GetKeyDown("9") && weapons.length >= 9) { SelectWeapon(8); selectedWeapon = 8; } else if (Input.GetKeyDown("0") && weapons.length >= 10) { SelectWeapon(9); selectedWeapon = 9; } }
function SelectWeapon (index : int) { for (var i : int=0 ;i<weapons.length; i++) { if (i != index){ weapons[i].gameObject.BroadcastMessage("deselectWeapon"); //the function deselectWeapon is in my gun script, and turns them off.
}else{
weapons[i].gameObject.BroadcastMessage("selectWeapon");
//the function selectWeapon is in my gun script, and turns them on.
}
}
}
whoa thanks a bunch man (or girl) expect a lot more questions from me in the future :)
No problem! I made a package for making FPS games (handling weapons and such), so I can easily just pull stuff from my code which I know works.
Sorry but i am to late with this reaction. I'm using this script. But when i press 1 for the weapon, the game gives me an error. This is the error: Broadcast$$anonymous$$essage selectWeapon has no receiver! UnityEngine.GameObject:Broadcast$$anonymous$$essage(String) Error in line 45
how can i turn the gun on and off?
Well, obviously this is because the message doesn't have a receiver? The error message tells all! Do you know how Send$$anonymous$$essage works? It calls a function on that object which has the name specified by the string paramater. In this case, "selectWeapon". You need to have a script on the weapon in question which includes the function "selectWeapon()". You can implement it however you choose.
Answer by Kona · Feb 15, 2011 at 05:03 PM
I use C# myself so can't give you any example code but what I would try in this case is to create a list in your characters class where you store all weapons you have picked up.
Then set up a way to swap between the weapons in that list when pressing a button, and for the weapon currently equiped, put it in a temp cache and remove it from the inventory. And set up another button for droping a weapon, for that simply remove the equiped weapon from the cache (since it's already removed from the inventory as that's done when equiping it).
oh and before equiping a new weapon ofcourse you'ld add the currently equiped one back to inventory so we don't have two weapons equiped or accidentally destroy the equiped one completly.
:) And I suppose when pressing the button to swap weapons I'ld probably call another method aswell that would instantiate the prefab/model of the currently equiped weapon at the point where the weapon should be positioned, and then make it a parent of the player character / camera
whoa thanks a bunch man (or girl) expect a lot more questions from me in the future :)
Your answer
Follow this Question
Related Questions
Switch Weapon Script Not taking effect HELP!! 2 Answers
Total Bullets to weapon reload? Help 1 Answer
Bullet Penetration 1 Answer
weapon manager 1 Answer