|
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?
(comments are locked)
|
|
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.
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;
}
}
}
(comments are locked)
|
|
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; 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".
(comments are locked)
|

gunObject.transform.parent = theObjectYouWantToBeTheParent.transform;
http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html