hey guys is it possible to Grab Objects
i mean more loke call of duty when you see a weapon on the ground you can pick it up by pressing f
hou is this done and is it possible in unity becous i have looked arround but could not find it
can you help me plase
greatings
Wesley
I have made a simple Pick up script for my shooter game and want to share it ;)
here it is :
//for calculating player -> object distance
var PlayerObject : GameObject;
var maxPickUpDistance : int = 12;
private var distance : int;
//Weaponinfo
var bullets : int;
var clips : int;
var delay : int;
var damage : int;
var weapon : String = "";
//other
private var pickup = false;
function OnGUI(){
if (pickup) if(Weapon != "") GUI.Label(Rect(Screen.width/2-100,Screen.height/2,200,25),"Press 'F' to pick up the " + weapon);
}
function Update() {
PlayerObject = GameObject.Find("Main Camera");
distance = Mathf.Sqrt((gameCharacter.transform.position - transform.position).sqrMagnitude);
if(distance <= maxPickUpDistance) {
pickup = true;
if(Input.GetKeyDown("f")){
Player.Bullets = bullets;
Player.Clips = clips;
Player.Delay = delay;
Player.Damage = damage;
Player.Weapon = weapon;
Destroy(gameObject);
}
}
if(distance > maxPickUpDistance) {
pickup = false;
}
}
just attach this script to the weapon that is in the scene and that you want to pick up. fill in the variables like the damge the weapon must do and the name of the weapon. it will be sended to a script callt player so change your script (where you handle the shooting code) to player or change in this code to your script name.
hope this helps. if you want to drop your old weapon for the new one just say it.
Lemon,