- Home /
Gun swap through pickup?
This is hard to explain but here we go. I have 2 guns parented to the main camera, the main camera is parented to an empty gameobject that holds the cameras animations. These 2 guns are on 2 separate fps controllers, I have then made a prefab of both of these plus one that doesn't have any gun at all just the cameras animations.
With this is there any way to swap guns with one that is on the ground that when the player walks over swaps them round? This is the easiest way I can describe what I'm trying to achieve, anyone who can help I would be very grateful.
Answer by Radon · Aug 08, 2012 at 11:02 PM
I have had the same question, and thankfully, @AldoNaletto had the answer for it.
http://answers.unity3d.com/questions/294476/weapon-pickup-and-change.html
I'm not the best of scripters and I'm struggling to make sense of of it, could you tell me how it works and such?
Answer by Radon · Aug 10, 2012 at 01:47 AM
Use this code by @AldoNaletto, First you need to have 2 weapons, primary and secondary. Tag the primary one "MachineGun" and the secondary "RocketLauncher". After applying the code on your controller, put a check mark next to MachineGun. Now, put your secondary weapon on the floor as a gameobject and tag it "RocketLauncher". Create any collider and re-size it to be big. Check the "Is trigger". Then play your project, walk over your weapon on the floor, press "f", and the magic should happen hopefully. Don't forget to set your primary and secondary variables as your weapons in the inspector.
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
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'm only having one gun to be held at a time so how could it be changed so you only have a primary weapon? Plus having a primary and secondary it sort of glitches out.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gun swap through collision 1 Answer
copy settings from other gameobject 2 Answers
Camera Recoil,Please help me. 2 Answers
How to make a pick up gun script? 0 Answers