Pick Up The Gun

So basicly my question is how can you make a gun/object be the child of the player when the player comes closer to the object/gun and presses F button to pick it up?

  • Felipe

Usually the object you pick is a fake one: it has a trigger volume, and when the player enters the trigger and presses F, the object is destroyed - but the player script “knows” that the object was picked.

The weapons are usually previously childed to the player and kept deactivated. When you pick a weapon, you just set a variable that tells you’ve got it, so it can be activated when you switch weapons. For other objects, you just increment a counter for each type - take a look at the example below (player script):

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

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

Like Aldonaletto said, you can put a dublicate of gun without any script.
And give it a script for destroying object when player is in trigger stay and press “f”.
After that you can give player something like that (which i am using in my game
also;

static var onetimeTorch : boolean = true ;
var torch11 : GameObject;
var hand1 : GameObject ;
static var callTorch : boolean = false;

function Update()
{
   if(Input.GetKeyUp("t"))
   {
       callTheTorch();
   }
   if(torchDestroy.getTorch == true && callTorch == true)
   {
     torch11.transform.position = hand1.transform.position;
     torch11.transform.rotation = hand1.transform.rotation;
   }
   else
   {
     torch11.transform.position = Vector3(-100,-100,650);
   }
}

function callTheTorch()
{
	if(onetimeTorch == true)
	{
	   callTorch = true;
	   onetimeTorch = false;
	}
	else
	{
	   callTorch = false;
	   onetimeTorch = true;
	}
}

hand1 : you can parent a empty game object to player’s hand and select it as hand1.

torch11 : this is the gun, when press “t” key gun gets to hand1. If press “t” again it goes to the (-100,-100,650) coordinates. You can change it to “f”.

i can understand what you mean @aldonalleto but ur script is referring to the fps tutorial…So say im not using the FPS tutorial how can i have this script changed to fit my game? As in everything same as how u said it above but only thing is that its not supposed to enable the fps tutorial guns but my guns…any help will be appreciated thanks :slight_smile:

I’m also trying to figure this thing out and this is what i have come up with hit

The gun prefab is not a child of the player, basically i have this Pistol Game Object which just displays the current gun sprite and my script just gives the gun sprite it has when the player comes near and presses the button H.

(Btw, if your using a rigidbody to move the Player you should be fine. If you’re using Raycasting, then you might want to add the a child to the player which has a boxcollider2D over the player and a rigidbody attached with no gravity)

Sorry, for the lengthy explanation, here’s my script -

using UnityEngine;

public class GunPickup : MonoBehaviour
{
    public SpriteRenderer PistolRenderer;
    public Player Player; //Player is the script which controls the player

    private SpriteRenderer _spriteRenderer;

    public float FireRate;
    public bool IsSingleBurst; //this is just a variable which tells if the gun is an automatic or a non-automatic

    public void Awake()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
    }

    public void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.GetComponentInParent<Player>() != null && Input.GetKeyDown(KeyCode.Q))
        {
            PistolRenderer.sprite = _spriteRenderer.sprite; // assigns this gun to the player's pistol
            Player.FireRate = FireRate;
            Player.IsSingleBurst = IsSingleBurst;
            if (BurstWhenTaken != null)
                Instantiate(BurstWhenTaken, transform.position, transform.rotation);
            Destroy(gameObject); // destroys this object
        }
    }
}

I am not able to assign a weapon to player on start. for example, if i start the game, the weapon should be loaded for use . Right now I am unarmed and I have to collect weapon to use it. any clue ?

is there any way, that my player starts a game with weapon in his hand. right now I have to collect weapons first, then I am able to use it?