Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Caldera12 · Mar 24, 2018 at 05:12 AM · gameobjectcollidertriggers

How to pick up a game object using E

Hi guys I'm trying to find out a way where i can pick up and item that appears after i shoot a box. But i cant find out a way to do it. This is my code for breaking a box.

public GameObject Drop;

 void OnTriggerEnter(Collider col)
 {
     if (col.gameObject.tag == "Bullet")
     {
         Destroy(col.gameObject);
         Instantiate(Drop, transform.position, transform.rotation);
     }
 }
Comment
Add comment · Show 4
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 hexagonius · Mar 24, 2018 at 06:19 AM 0
Share

and what exactly is your problem?

avatar image Caldera12 hexagonius · Mar 24, 2018 at 06:31 AM 0
Share

When the item appears i want to be able to walk over to it and be able to press e to pick it up like a weapon of some sorts.

avatar image hexagonius Caldera12 · Mar 24, 2018 at 11:47 AM 0
Share

how exactly? looking at it? standing in it?

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rakshaan · Mar 24, 2018 at 01:41 PM

after you break the box an Item is dropped. There are two ways. You can use collision or on trigger functions to sense when the player is near or on top of the pickup. Or you could use a float like the following:

float dist = Vector3.Distance(drop.position, Player.transform.position);
if(dist <= 1)

{
Destroy(drop.gameObject);
//add code here to do what you want after the box is destroyed eg. add something to your inventory }

this code finds if your player is 1 or less in distance. if so destroy the box to make it seem if it is picked up.
after the box is destroyed you need to simulate what you want the box to do when it is picked up. like after the box is destroyed add 10 health to the player or add the box to the inventory.

I hope this was helpful :)

Comment
Add comment · 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
0

Answer by LeeXxE · Mar 24, 2018 at 01:53 PM

Check this video, might help https://www.youtube.com/watch?v=Z7-H91qrxfo&t=479s

Comment
Add comment · 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
0

Answer by Priyanka-Rajwanshi · Mar 26, 2018 at 07:15 AM

@Caldera12

Try this:

      public float radius;
      if (Input.GetKeyDown(KeyCode.E))
      {
         Collider[] droppedBox = Physics.OverlapSphere(transform.position, radius);
         if (droppedBox != null)
         {        
             for (int i = 0; i < droppedBox.Length; i++)
             {
                 if (droppedBox[i].transform.CompareTag("Drop"))
                 {
                    //your code here
                     Destroy(droppedBox[i].transform.gameObject);
                 }
             }
         }
     }

The dropped item should have a tag named 'Drop'.When the player comes within the radius of dropItem, and player presses E, the droppedItems are collected(destroyed).

Comment
Add comment · 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
0

Answer by YetiMoon · Mar 27, 2018 at 02:04 PM

@Caldera12

So, you need to instantiate a prefab when the box is destroyed (the pickup-able item) with two components attached; a box collider (marked as "is trigger") and a script (PickupScript or something).

When the pickup is instantiated the trigger box will allow you to "do something" when the player is inside the trigger and presses a certain key. Additionally, you should not hard code keys but rather use Input.GetButton as this allows you to offer the player keybindings (so they ultimately choose which key acts as the interact/pickup button).

  if (Input.GetButton("PickupItem"))
             {
                 // do something
             }


pickupscript.cs

     public GameObject pickupableItem;
 
     void OnTriggerEnter(Collider col) {
 
         // confirm trigger was activated
         Debug.Log("Player is within pickup range")
 
         // if player presses pickup key (set default to 'e')
         if (Input.GetButton("PickupButton") {
 
             // confirm key press was registered
             Debug.Log("Player pressed pickup key");
             Debug.Log("Player picked up item");
             // destroy the object
              Destroy(pickupableItem);
 
         }
     
     }
     
     void OnTriggerExit(Collider col) {
     
         Debug.Log("Player is outside pickup range")
 
     }

However, you would still need to handle "adding" the item to the player's inventory I presume?

There are multiple ways you could do this, such as having a simple InventoryManager.cs script attached to an empty gameobject in your scene that keeps track of what items the player has available... or attach a PlayerManager/InventoryManager.cs script to the player itself and do it that way.

Comment
Add comment · 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

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

127 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

My bullets destroys Objects without it hitting it. 1 Answer

Raycast starts ignoring gameobject after a few seconds 0 Answers

Problems with the Trigger Collider (randomly fictional) 1 Answer

Opening a door when a button object has something it it's collider. 0 Answers

Locate a game object using rays 1 Answer


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