x


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
more ▼

asked Jan 28 '12 at 06:03 PM

BigBlob gravatar image

BigBlob
340 37 62 66

gunObject.transform.parent = theObjectYouWantToBeTheParent.transform;

http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html

Jan 29 '12 at 06:19 PM vatsalAtFEI
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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

answered Jan 29 '12 at 10:21 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 29 '12 at 10:40 PM

yigo2413 gravatar image

yigo2413
16 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x806
x446

asked: Jan 28 '12 at 06:03 PM

Seen: 1046 times

Last Updated: Jan 29 '12 at 10:48 PM