- Home /
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;
}
I have the same Problem, tried googling, YouTube, form etc but there just seems to be no solution
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!