Weapon Switching Script Input

Hey all,

So i am in the process of wrapping my head around the basics of getting a FPS nailed down solid and in the process i have come across the need to create a script that allows the player to switch between weapons quickly and easily. So i did some research on the subject and i noticed that a lot of the people simply attach all of the models of all the weapons to the camera and then deactivate them until they’re called upon by a keyboard input of some form.

I got to thinking about ways i could put my own spin on this script to add a little more pizzazz to it and throw in my own little touch. I came up with the idea of creating prefabs out of all the weapons then writing a script that would simply spawn a clone of the default weapon, whenever someone chose the next weapon it would delete the prefab clone of the primary gun, and add in the prefab clone of the new gun.

I’m really just looking for feedback from people who are more learned than me. What do you think of this concept? What are some of the potential complications with this method of going about the weapons if any?

Thanks in advance!

P.S. Here’s the code for those of you who would like to take a look at what i’m getting at.

var weapons : GameObject[]; 
var selectedWeapon : int;
var eWeap : GameObject;

/*We're going to instantiate the default weapon in the Start function and initialize the value of selected weapon to that.*/
function Start()
{
	eWeap = Instantiate(weapons[0], transform.position, transform.rotation);
			eWeap.transform.parent = transform;
			selectedWeapon = 0;
}
function Update () {
	/*We're setting up some if statements that will detect the keyboard inputs, destroy the old clone, bring in the new one, and change the selectedWeapon value to the appropriate value.*/
	if(Input.GetKeyDown("1"))
	{
		if(selectedWeapon == 1)
		{
			Destroy(eWeap,0);
			eWeap = Instantiate(weapons[0], transform.position, transform.rotation);
			eWeap.transform.parent = transform;
			selectedWeapon = 0;
		}
	}
	else if(Input.GetKeyDown("2"))
	{
		if(selectedWeapon == 0)
		{
		  Destroy(eWeap,0);
		  eWeap = Instantiate(weapons[1], transform.position, transform.rotation);
		  eWeap.transform.parent = transform;
		  selectedWeapon = 1;
	  	}
	}
}

If you were going to try this method, you would have to make two variables one for position and another for a rotation
the vector 3 and quaternion would have to follow where the guns parent is, so you’re using javascript i would make a script like,

var WeapParentPosition : Transform;
var WeapParentRotation : Transform;

function start()
{
	WeapParentPosition = transform.localPosition
	WeapParentRotation = transform.localRotation
}

that would give you the position of where the prefab should go, then just Instantiate the prefab to the WeapParentPosition and WeapParentRotation,

to get rid of the previous weapon just use destroy

I would not use this method. Tried almost all possible ways to switch weapons, and this one isn’t best choice.

Could you provide me with a little more information why? I mean, i got the method 100% operational and functioning on my end but you seem to be the one with more experience so i would be interested as to why it’s not the best choice.

I got by the parent location issues by making an empty gameobject that is attached as a child to the main camera on the first person controller. This empty gameobject has the script i linked above attached to it which makes all the prefabs spawn as a child of that object. Thus the location and rotation of the camera are already accounted for when they spawn. Thanks for the input!

It means you wrote separate script which holds all info about current weapon (which you destroy)? Isn’t simpler just disable current weapon Renderer and deactivate script?
Maybe you saw how its done in “FPS Kit 2”?

I skimmed the second FPS kit but i didn’t use much of the information from it. I did write a seperate script for the weapon to be fired and such, It works well for me, i like to have everything nice and compartmentalized so if something goes wrong i know exactly where to look. Granted i know it has it’s downsides but that’s how my brain functions.