• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Radon · Aug 01, 2012 at 09:12 PM · javascriptobjectweaponpickup

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;
     }        
   }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radon · Aug 02, 2012 at 03:42 AM 0
Share

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

avatar image Radon · Aug 03, 2012 at 07:07 AM 0
Share

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.

avatar image aldonaletto · Aug 04, 2012 at 01:33 AM 0
Share

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

avatar image Radon · Aug 04, 2012 at 03:34 AM 0
Share

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.

avatar image aldonaletto · Aug 04, 2012 at 10:45 AM 0
Share

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").

Show more comments
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radon · Aug 01, 2012 at 10:33 PM 0
Share

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.

avatar image Radon · Aug 02, 2012 at 02:28 AM 0
Share

Do I have script error or is it something I'm doing wrong?

avatar image Chris12345 · Aug 04, 2012 at 02:28 AM 0
Share

if (inTrigger && Input.Get$$anonymous$$eyDown("f")){ //needs to be if// (inTrigger && Input.Get$$anonymous$$eyDown("1")){

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Storing and using weapons and objects 0 Answers

Weapon pick up and switching script 2 Answers

Buff System 1 Answer

How to pick / drop object 1 Answer

Weapon pickup Script...HELP!(Java) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges