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;
}
}
}