- Home /
Weapon Pickup and change
I'm using the code below to "pick up" an object with in trigger. I'm using the SetActiveRecrusivily to activate and deactivate objects. But now, when my "Secondary" is activated after "pick up", I cannot activate my primary when key (1) is down. Please help
var hasRocketLauncher = false; // tells if you have a rocket launcher
var hasMachineGun = false; // tells if you have a machine gun
var ammoClips = 1; // tells how many ammo clips you have
var rockets = 10; // tells how many rockets you have
var Primary : GameObject;
var Secondary : GameObject;
private var inTrigger = false;
private var object: Transform;
function OnTriggerEnter(other: Collider){
inTrigger = true; // the player entered the trigger
object = other.transform; // save the object transform
}
function OnTriggerExit(other: Collider){
inTrigger = false; // the player left the trigger
}
function Update(){
// if player inside trigger and F pressed:
if (inTrigger && Input.GetKeyDown("f")){
switch(object.tag){
case "MachineGun":
hasMachineGun = true; // enable switching to the machine gun
Destroy(object.gameObject); // destroy the picked object
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
if(Input.GetKeyDown("1")){
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
}
break;
case "RocketLauncher":
hasRocketLauncher = true; // enable switching to the rocket launcher
Destroy(object.gameObject); // destroy the object
break;
case "AmmoClip":
ammoClips++; // increment ammo clips
Destroy(object.gameObject);
break;
case "Rocket":
rockets++; // increment rockets
Destroy(object.gameObject);
break;
}
}
Answer by aldonaletto · Aug 02, 2012 at 02:54 AM
You should not place the weapon switching code inside that if, as @ThermalFusion noticed - you should press F and 1 in the same frame!
Add a function to select the weapon (SelWeapon) and call it when the corresponding weapon is picked - this will implement automatic switching to the newly picked weapon (not always a good idea...). Place the code to check keys 1 and 2 after that if, and call SelWeapon according to the key pressed:
... function Update(){ // if player inside trigger and F pressed:
if (inTrigger && Input.GetKeyDown("f")){
switch(object.tag){ case "MachineGun": hasMachineGun = true; // enable switching to the machine gun Destroy(object.gameObject); // destroy the picked object SelWeapon(1); // select primary weapon automatically break; case "RocketLauncher": hasRocketLauncher = true; // enable switching to the rocket launcher Destroy(object.gameObject); // destroy the object SelWeapon(2); // select secondary weapon automatically break; case "AmmoClip": ammoClips++; // increment ammo clips Destroy(object.gameObject); break; case "Rocket": rockets++; // increment rockets Destroy(object.gameObject); break; }
} // Code to switch weapons when 1 or 2 is pressed: // - key 1 selects primary weapon: if (Input.GetKeyDown("1")) SelWeapon(1); // - key 2 selects secondary weapon: if (Input.GetKeyDown("2")) SelWeapon(2); }
// function used to select weapon: activates only the selected weapon and // deactivates all others; only activates a weapon if the player already has it
function SelWeapon(nWeapon){ Primary.SetActiveRecursively(nWeapon == 1 && hasMachineGun); Secondary.SetActiveRecursively(nWeapon == 2 && hasRocketLauncher); }
I applied your code. Now, when I press "2" $$anonymous$$y Secondary gameobject is not getting activated. But my Primary is which is good. When I press "1" again I see my primary again but still no secondary when I press 2
So now I have two errors $$anonymous$$r. Naletto. First, I could switch weapons before I pick up the second one. The second weapon doesn't appear when I press 2.
This SelWeapon function is very simple, and allows weapon switching even when you don't have the other weapon - you end with bare hands when switching to a disabled weapon. A better SelWeapon function should allow switching only when you have the weapon to switch to:
... // drag here a sound to indicate "no such weapon": var noWeaponSound: AudioClip;
function SelWeapon(nWeapon){ var canSwitch = false; switch (nWeapon){ case 1: //canSwitch = true if machine gun enabled canSwitch = has$$anonymous$$achineGun; break; case 2: // canSwitch = true if rocket launcher enabled canSwitch = hasRocketLauncher; break; } if (canSwitch){ // if the weapon is enabled... // activate the selected weapon and deactivate the other(s): Primary.SetActiveRecursively(nWeapon == 1); Secondary.SetActiveRecursively(nWeapon == 2); } else { // if not enabled, play some warning sound: audio.PlayOneShot(noWeaponSound); } } ...
Here is the problem now. When I start, I press 2, and the no weapon sound appears and my "$$anonymous$$achineGun" is active. However, When I pick up the weapon on the ground by pressing "f", my RocketLauncher is not getting check and so I still can't switch my weapon after pick up.
The weapons you pick must have the right tags: "$$anonymous$$achineGun" and "RocketLauncher" in this code - these tags are recognized by the picking code and enable the corresponding weapon. The same applies to the ammo ("AmmoClip" and "Rocket").
Answer by ThermalFusion · Aug 01, 2012 at 10:20 PM
Either there's a problem with the object being destroyed and thus setting isTrigger to false, which will make everything in update to not happen because of if (inTrigger && Input.GetKeyDown("f")). if(Input.GetKeyDown("1")) is also nested inside the block above, which means you have to press both f and 1 at the same time.
Ok, so I tried many things. First, I put my if(Input.Get$$anonymous$$eyDown("1")) function ABOVE the (inTrigger && Input.Get$$anonymous$$eyDown("f")) but still didn't work. I used the original code I posted and pressed both "f" and "1" but still didn't works. And. my object "Primary" is not getting destroyed because I can see it deactivated in my inspector AFTER I "pick up" the object.
if (inTrigger && Input.Get$$anonymous$$eyDown("f")){ //needs to be if// (inTrigger && Input.Get$$anonymous$$eyDown("1")){
Your answer
Follow this Question
Related Questions
Buff System 1 Answer
How to pick / drop object 1 Answer
Storing and using weapons and objects 0 Answers
Weapon pick up and switching script 2 Answers
how to save an array into an object 1 Answer