Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Stand Alone OS · Oct 18, 2013 at 09:07 PM · weaponsswappick upswapping

How to drop my held weapon after picking one up?

Hey everyone! I've been using Unity for the past 2 weeks and im just learning java script so try not to judge me on my 'codding ability's'.

With that said im making a FPS zombie game and Im working on the combat system. I got my player spawning with a FiveSeven pistol and a Combat Knife, and I have a Spas 12 Shotgun on the ground. I can pickup the Spas12, but cant figure out how I would go about dropping my current weapon? I've attempted googling around but nothing came up.

If someone would help me out it would be muchly appreciated. Thank you.

My Code:

GunSwitchingScript (Attached to the "Spas 12 on the ground")

 var GunSwapScript;
 var Gun : GameObject;
 
 var GunName = " ";
 var PickUpText : GameObject;
 
 
 function Start()
 {
     GunSwapScript = GameObject.Find("Guns").GetComponent("GunSwap");
     PickUpText.guiText.text = "Press 'E' to pick up " + GunName;
     PickUpText.active = false;
 }
 
 function OnTriggerEnter (col : Collider)
 {
     PickUpText.active = true;    
 }
 
 function OnTriggerStay(col : Collider)
 {
     if(Input.GetKey(KeyCode.E))
         {
             if(GunSwapScript.PrimaryGunSelected == false)
             {
                 GunSwapScript.PrimaryGun.SetActiveRecursively(false);
                 GunSwapScript.secondaryGun.SetActiveRecursively(false);
                 GunSwapScript.secondaryGun = Gun;
                 GunSwapScript.secondaryGun.SetActiveRecursively(true);
                 PickUpText.active = false;
                 Destroy(this.gameObject);
             }else
             {
                 GunSwapScript.secondaryGun.SetActiveRecursively(false);
                 GunSwapScript.PrimaryGun.SetActiveRecursively(false);
                 GunSwapScript.PrimaryGun = Gun;
                 GunSwapScript.PrimaryGun.SetActiveRecursively(true);
                 PickUpText.active = false;
                 Destroy(this.gameObject);
             }
     }
 }
 
 function OnTriggerExit ()
 {
     PickUpText.active = false;
 }



GunSwapScript (Attached to a 'Gun' Empty GameObject which contains all my weapons)

 var FiveSeven : GameObject;
 
 var Spas12 : GameObject;
 
 var CombatKnife : GameObject;
 
 var PrimaryGunSelected = true;
 
 var PrimaryGun : GameObject;
 var secondaryGun : GameObject;
 
 
 function Awake()
 {
 //Declare Weapons
     //HandGuns
     FiveSeven.SetActiveRecursively(false);
     
     //ShotGuns
     Spas12.SetActiveRecursively(false);
     
     //AssualtRifles
     
     //Melee
     CombatKnife.SetActiveRecursively(false);
     
 //Code
     PrimaryGun = FiveSeven;
     secondaryGun = CombatKnife;
     
     PrimaryGun.SetActiveRecursively(true);
     secondaryGun.SetActiveRecursively(false);
 }
 
 
 function Update () 
 {
     if(Input.GetKey(KeyCode.Alpha1))
     {
         SwapPrimary();
     }
     
     if(Input.GetKey(KeyCode.Alpha2))
     {
         SwapSecondary();
     }
     
 }
 
 function SwapPrimary()
 {
     PrimaryGun.SetActiveRecursively(true);
     secondaryGun.SetActiveRecursively(false);
     PrimaryGunSelected = true;
 }
 
 function SwapSecondary()
 {
     PrimaryGun.SetActiveRecursively(false);
     secondaryGun.SetActiveRecursively(true);
     PrimaryGunSelected = false;
 }
 
Comment
Add comment · Show 1
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 me2000 · Jul 07, 2015 at 05:18 AM 0
Share

I have the same Problem, tried googling, YouTube, form etc but there just seems to be no solution

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WallzyGames · Mar 16, 2016 at 05:41 PM

Create and empty game object. Place it in front of the player. Attach it to weapon player equips. Here is script you attach to the Empty Game Object you made

 var gunDrop:Transform;
 var playersGun:GameObject;
 var dropForce:float;
 var amountDropped : int = 0;
 var maxDroppables : int = 99999;
 var shootSound : AudioClip;

 function Update()
 {
     if(Input.GetKeyDown(KeyCode.G) && amountDropped < maxDroppables)
     {
         var instanceBullet = Instantiate(gunDrop, transform.position, Quaternion.identity);
         instanceBullet.GetComponent.<Rigidbody>().AddForce(transform.forward * dropForce);
         GetComponent.<AudioSource>().PlayOneShot(shootSound);
         amountDropped++;
 
         playersGun.SetActive(false);
     }
 }

Drop Weapon by pressing G. I got lazy and just added 99999 amount of weapons that you can instantiate but it does give you a little more to control if you want an object to not be usable once you drop it. I hope this helps!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

How to swap 2 gameobjects after 2 click! 4 Answers

Swapping one model for a prefab 2 Answers

Swapping Items in List Replaces Value Instead 1 Answer

How do you dynamicly change a cameras culling mask based on an object selection? 1 Answer

Swap player positions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges