- Home /
pickup items please help
hi i need anyone help me
please
i have items ( radio ) i need the player Fps to pick up ...i searched many website and tutorial nothing found
..... what i want i need steps by steps how i make my player pick up any items
thanks alot support unity3d
here example : 1 - http://www.mediafire.com/view/?fm4fn40dgy3pzlu# 2- http://www.mediafire.com/view/?426bh2fc2a74odw 3- http://www.mediafire.com/view/?rvpm66i5povd8rt 4- http://www.mediafire.com/view/?qv1xb5251b0njob 5- http://www.mediafire.com/view/?sv3ev9kzx6p9n33# 6- http://www.mediafire.com/?0058cebhbg7a0hd
Answer by Unityprogramer10 · Aug 15, 2012 at 05:38 AM
well im not going to do a full tutorial but
1 you tag the item that you can pick up as "pickupableitem"
in the player object put two variables for each weapon/item
2 in your script raycast to see if you hit an object with the tag "pickupableitem"
3 in your playerobject out an array with all the weaponprefabs
4 in the pickupableitem you put a script with the index of the array which corresponds with the item
5 then you Instantiate the object from the array destroy the one in the map
6 Instantiate the weapon variable from the array
7 parent it to the player
8 delete whta ever object you were holding and Instantiate it with a rigid body and add force
Hope this helps
If some thing dosnt make sense ill explain it as well as i can
Thanks alot for helping i try but i still confused ....if you have time can you explain more , that's why i told you i need step by step
i have fps player + radio object i create collider box in radio object i create java script also i create tage name (pickup) i attach script with player + and attach tag with radio object and also i marked in fps ( is trigger )
so is this correct or wrong
if correct then i need script for object that he can pick it up only ....not destroy or throw it ...only just pickable
thanks that's why i need support
:)
You need to define the position where the radio will be held on the hand, then when picked up you translate its position from the ground to the hand, remove the collider and then attach it to the hand. Now you are done. Your radio will move with your guy.
cany you check here ? http://answers.unity3d.com/questions/302526/pickup-items-please-continue.html
Answer by Unityprogramer10 · Aug 16, 2012 at 12:53 AM
using UnityEngine;
using System.Collections;
public class weaponchange : MonoBehaviour {
public Transform[] weaponlist;
public Transform spawnpoint;
private GameObject Pickup
private Float num;
void Update(){
Ray ray = maincamera.camera.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2,0));
RaycastHit hit;
if(Physics.Raycast(ray, out hit,pickuprange)) {
if (hit.collider.gameObject.tag == "pickable"){
Pickup = hit.collider.gameobject
num = hit.collider.gameObject.getComponent<weaponnum>().num;
}else{
Pickup = null;
}
if(Pickup != null){
Transform pickupob = insatainte(weaponnum[num],spawnpoint.position,spawnpoint.rotation) as Transform;
pickupob.parent = transform;
Destroy(Pickup):
}
}
}
sorry its in c# but idk javascript well
just put a script on the radio with a public variable named num and put the radioprefab into the array and make sure the num variable = the array index
hope this helps im bad at explaing though, but you can look up the commands on the unity reference
unityprogramer : thanks alot but you know what ....your code make me confused .....i don't mean bother you....no ...i must thank you for your hard helping me thank you alot
well this is pretty simple code for a pick up if you really dont understand it you should watch some tutorials about raycasting or try something simpler.
Answer by aldonaletto · Aug 16, 2012 at 12:37 AM
From your 3rd picture, I can see that the radio collider is too small - the character will never touch it! Modify its size (Box Collider/Size) so that the collider becomes a cube about 1x1x1 units (or more), and the character can touch it from any side.
The script attached to the character must be something like this (similar to what @fafase wrote):
var hasRadio = false; // tells whether the player has radio or not
function OnTriggerEnter(other: Collider){ if (other.tag == "pickup"){ hasRadio = true; // now player has got a radio! Destroy(other.gameObject); // destroy the radio object } }
EDITED: Well, since you prefer to use a distance method, I modified your script to only check distances to the pickable objects when you press Q - this will not impact your frame rate, because the distance checking will only be done in the frame where Q was pressed. I also changed ObjectsToPick to a builtin array instead of a List: it's faster, and you will not need a List anyway - removing the picked items inside the for..in loop could screw up the loop control. Instead of removing a picked item, this script just ignores the inactive ones (the items are deactivated when picked up). It also ignores items behind the player, and finishes the loop when an item is picked up to avoid picking other close items, if any:
import System.Collections.Generic;
var Inventory : List. = new List.< GameObject>(); var pickUpDistance : float = 5f; var pickSound : AudioClip; // drag the sound here
private var ObjectsToPick : GameObject[];
function Start () { // get the pickable objects in a builtin array: ObjectsToPick = GameObject.FindGameObjectsWithTag("PickUps"); }
function Update () { if (Input.GetKeyDown("q")){ // only check distances when q pressed var here: Vector3 = transform.position; // get the character position in a variable for (var Item : GameObject in ObjectsToPick) { // for each pickable item... if (Item.active){ // that's still active... // get the vector player->item: var dir: Vector3 = Item.transform.position - here; // if item close enough and not behind the player... if (dir.magnitude < pickUpDistance && Vector3.Dot(dir, transform.forward) > 0) { // play the sound... AudioSource.PlayClipAtPoint(pickSound, Item.transform.position); // deactivate the object... Item.SetActiveRecursively(false); // add it to the inventory... Inventory.Add(Item); // and finish the loop: break; } } } } }
thanks for helping mr.aldo i found this script and it's work ...here let me show you
in java script
import System.Collections.Generic;
var Inventory : List.<GameObject> = new List.<GameObject>();
var pickUpDistance : float = 5f;
private var ObjectsToPick : List.<GameObject> = new List.<GameObject>();
function Start () { ObjectsToPick.AddRange(GameObject.FindGameObjectsWithTag("PickUps")); } function Update () { if (ObjectsToPick.Count > 0) { for (var Item : GameObject in ObjectsToPick) { if (Vector3.Distance(transform.position, Item.transform.position) < pickUpDistance) { if (Input.Get$$anonymous$$eyDown("q")) { Item.SetActiveRecursively(false); Inventory.Add(Item); ObjectsToPick.Remove(Item); } } } } }
can you do me a favor i want to add sound after he pickup radio i need code script in java script :)
That's a terrible method! It iterates through all items each Update, what may become very slow if you have lots of PickUp items! Anyway, you can play the sound with PlayClipAtPoint right before SetActiveRecursively:
var sound: AudioClip; // drag the sound here
... if (Input.Get$$anonymous$$eyDown("q")){ AudioSource.PlayClipAtPoint(sound, Item.transform.position); Item.SetActiveRecursively(false); ...
yeah i know that... it will maybe very slow but what can i do if you see this problem my code ....if you have time ..sorry to ask you many time but i want to learn more about this please mr.aldo if you see my code will be crash my game or slow ...please correct my code
........................................... also i copy your code sound clip and paste in my code (pickup.js) and i attach my sound clip for example ....i run my game and it show problem
here some photo
mediafire.com/view/?w5we5dfbx9mbfsf mediafire.com/view/?0xos3uhc7vjldg8
I fixed your script and improved its performance - take a look at my edited answer.
Answer by kdd_9000 · Aug 16, 2012 at 04:08 AM
I have a scripting tutorial on this, I wrote the code in C# in the video but you can download the code in either C# or JAVASCRIPT if you want. The video goes over item pick ups like the code the others suggested. Here's the link http://youtu.be/Pc2uIIlzYes
thanks alot but this is not what i mean i mean when he reach to the radio the player must press button let's just say on keyboard (e) when he pick it up the player hear sound clip it means he pickup radio
thanks alot but this is now what i want
Your answer
![](https://koobas.hobune.stream/wayback/20220613075705im_/https://answers.unity.com/themes/thub/images/avi.jpg)