- Home /
Weapon switching
i have this code from the fps toutorial
it works perfect
now i want to switch more than 2 weapons how can ideal with it
i tried many things but nothing worked
here is the script
function Start () { // Select the first weapon SelectWeapon(0); }
function Update () { // Did the user press fire? if (Input.GetButton ("Fire1")) BroadcastMessage("Fire"); if (Input.GetKeyDown("1")) { SelectWeapon(0); }
else if (Input.GetKeyDown("2")) { SelectWeapon(1); }
}
function SelectWeapon (index : int) { for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }
Answer by duck · Apr 17, 2010 at 10:17 AM
Well, from the code you've posted, it looks like you'd need to:
Add a new weapon as a child object of your player
(Since the code assumes that each child of the player object is a weapon option, and this is how it accesses them)
Modify the 'if..else' part of you script so that instead of:
if (Input.GetKeyDown("1"))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown("2"))
{
SelectWeapon(1);
}
You add some more key options, like this:
if (Input.GetKeyDown("1"))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown("2"))
{
SelectWeapon(1);
}
else if (Input.GetKeyDown("3"))
{
SelectWeapon(2);
}
else if (Input.GetKeyDown("4"))
{
SelectWeapon(3);
}
If this doesn't work, post a more detailed description of the problems you're having with it, and I'll try to help further.
Cool :) Remember to accept and upvote the answer, if it worked for you.
Answer by Geoff 1 · Apr 17, 2010 at 03:25 PM
Duck seems to be right, what you could also do, is just have 2 as go to the next weapon, 1 go back a weapon. Like this:
var wepNum : float = 0;
var numOfWeps : float = 5;
//whatever the rest is
function Update () {
SelectWeapon(wepNum);
}
if(Input.GetKeyDown("1"))
{
if(wepNum > 0)
wepNum--;
}
if(Input.GetKeyDown("2"))
{
if(wepNum < numOfWeps) {
wepNum++;
}
//Rest of code required
Answer by BlackHunter · Sep 11, 2012 at 07:12 PM
if (Input.GetKeyDown("1")) { SelectWeapon(0); } else if (Input.GetKeyDown("2")) { SelectWeapon(1); } else if (Input.GetKeyDown("3")) { SelectWeapon(2); } else if (Input.GetKeyDown("4")) { SelectWeapon(3); } else if (Input.GetKeyDown("6")) { SelectWeapon(5); }
Answer by AskMealirko321 · Jun 04, 2013 at 11:07 PM
Here Is A Script To Switch To 5 Weapons Don't Assign Ticks :D I Recommend You 1-Knife.2-Pistol.3-Assault Rifle.4-Machine Gun.5-Sniper :D :D GOOD LUCK X_X
var tick1 : AudioClip;
var tick2 : AudioClip;
var tick3 : AudioClip;
var tick4 : AudioClip;
var tick5 : AudioClip;
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) {
audio.PlayOneShot(tick1);
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
audio.PlayOneShot(tick2);
SelectWeapon(1);
}
else if (Input.GetKeyDown("3")) {
audio.PlayOneShot(tick3);
SelectWeapon(2);
}
else if (Input.GetKeyDown("4")) {
audio.PlayOneShot(tick4);
SelectWeapon(3);
}
else if (Input.GetKeyDown("5")) {
audio.PlayOneShot(tick5);
SelectWeapon(4);
}
}
function SelectWeapon (index : int) {
for (var i=0;i
Your answer
Follow this Question
Related Questions
gun switch script 26-feb-2010 1 Answer
In-Game Weapon store 3 Answers
Weaponbob for first person shooter 1 Answer
Is there a way to make a selected object a child of another object during the game? 1 Answer
bow and arrow (load and fire) 1 Answer