- Home /
weapon pickup and equiping it to FPS player
I know this has been asked but I can't find an answer that suits what I'm trying to do.
I have a setup where my FPS player starts off in a house with no weapon selected.
He finds on on the wall and picks it up. I'm using this as my pickup for the weapon:
var gotShotgun : boolean = false;
var gunPickupSound : AudioClip;
function Start (){ //playerWeapons is the players selectable weapons playerWeapons = this.GetComponent("PlayerWeapons"); }
function OnTriggerStay (){
if (Input.GetButtonDown("Pickup")){
gotShotgun = true;
Destroy(gameObject);
//Play Gun Pickup Sound
if (gunPickupSound)
AudioSource.PlayClipAtPoint(gunPickupSound, transform.position);
print("You Picked Up The Double Barel Shotgun");
}
}
The gun dissapears from the wall and it prints out that you have "You Picked Up The Double Barel Shotgun"
So far so good.
What I can't figure out is how to get it to activate and load up the shotgun from my PlayerWeapons script
var weapons : GameObject[]; var selectedWeapon : int;
function Awake () { // Select the first weapon SelectWeapon(0); }
function Update () { // Did the user press fire? if (Input.GetButton ("Fire1")) BroadcastMessage("Fire");
if (Input.GetKeyDown("1") && weapons.length >= 1) { if(!weapons[0].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(0); selectedWeapon = 0; } } else if (Input.GetKeyDown("2") && weapons.length >= 2) { if(!weapons[1].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(1); selectedWeapon = 1; } } else if (Input.GetKeyDown("3") && weapons.length >= 3) { if(!weapons[2].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(2); selectedWeapon = 2; } } else if (Input.GetKeyDown("4") && weapons.length >= 4) { if(!weapons[3].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(3); selectedWeapon = 3; } } else if (Input.GetKeyDown("5") && weapons.length >= 5) { if(!weapons[4].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(4); selectedWeapon = 4; }
}
}
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); } }
3 months of trying to learn this code stuff and a lot of it still makes my brain hurt lol. Anyone know how I can make this work?
Answer by flaviusxvii · May 12, 2011 at 08:47 PM
First of all. This code:
if(!weapons[0].gameObject.GetComponent("weaponLockout").isLocked){
SelectWeapon(0);
selectedWeapon = 0;
}
Is a good candidate for a function:
function selectWeaponByIndex(var i:int):void
{
if(!weapons[i].gameObject.GetComponent("weaponLockout").isLocked) {
SelectWeapon(i);
selectedWeapon = i;
}
}
So when you want weapon "1" you can just call selectWeaponByIndex(1).
Now.. when you pick-up the shotgun you should broadcast a message that you did. Have your PlayerWeapons script respond to this by selecting the shotgun.
Thanks flaviusxvii, not sure I really get what you are doing here, can you perhaps explain in more detail for those of us code challenged ;)
I don't know what you're asking me to clarify? Functions? Broadcasting? You already have examples of both in your code. Use those as templates.
O$$anonymous$$ well, like I say, this code stuff a lot I don't get so when I changed my code to what you have here I get an error message that I don't know what to do about:
Assets/WeaponScripts/PlayerWeapons.js(15,10): BCE0044: expecting (, found 'selectWeaponByIndex'.
http://pastebin.com/idbGRjG$$anonymous$$ <-- Like this for the function change..
Ok I changed my script to that but it does not equip my gun to my player, so what am I missing? It took me a while just to see what was different about my script and the one you have.
Answer by Kashaunzilla · May 17, 2011 at 03:56 AM
Try this.
var shotGun = Transform; private var shotGun : machineGun;
function OnTriggerEnter () { Destroy (gameObject); if ( machineGun ) { machineGun.enabled = true; } }
All you have to do now is to make the mesh activate. More detail needed, let me know.
Your answer
Follow this Question
Related Questions
weapon pickup 1 Answer
How can I detect if box is touching a tag named weapon? 1 Answer
How do I fix my gun animation 0 Answers
PickUp Weapon in Brick Breaker Game 1 Answer
ramdomised weapon pickup 1 Answer