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

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 :slight_smile:

Check this video, might help

@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*.transform.CompareTag("Drop"))*

{
//your code here
Destroy(droppedBox*.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).

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