Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by taketime · Aug 15, 2012 at 03:00 AM · pickupitems

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taketime · Aug 15, 2012 at 06:59 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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"

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taketime · Aug 15, 2012 at 05:04 PM 0
Share

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

:)

avatar image fafase · Aug 15, 2012 at 05:07 PM 0
Share

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.

avatar image taketime · Aug 15, 2012 at 09:34 PM 0
Share

cany you check here ? http://answers.unity3d.com/questions/302526/pickup-items-please-continue.html

avatar image
2
Wiki

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Unityprogramer10 · Aug 16, 2012 at 12:54 AM 0
Share

hope this helps im bad at explaing though, but you can look up the commands on the unity reference

avatar image taketime · Aug 16, 2012 at 01:13 AM 0
Share

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

avatar image Unityprogramer10 · Aug 16, 2012 at 02:30 PM 0
Share

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.

avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taketime · Aug 16, 2012 at 01:11 AM 0
Share

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

avatar image taketime · Aug 16, 2012 at 01:14 AM 0
Share

can you do me a favor i want to add sound after he pickup radio i need code script in java script :)

avatar image aldonaletto · Aug 16, 2012 at 02:15 AM 0
Share

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); ...

avatar image taketime · Aug 16, 2012 at 02:56 AM 0
Share

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

avatar image aldonaletto · Aug 17, 2012 at 02:54 AM 0
Share

I fixed your script and improved its performance - take a look at my edited answer.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taketime · Aug 16, 2012 at 05:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to fix monobehavour-pickups together with class-copying 0 Answers

item pick up menu 1 Answer

Collecting items when you destroy something? 1 Answer

Using enums to design an inventory system? 1 Answer

How i can make a script-made button interactable? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges