- Home /
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);
}
}
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.
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 :)
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
Answer by Priyanka-Rajwanshi · Mar 26, 2018 at 07:15 AM
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).
Answer by YetiMoon · Mar 27, 2018 at 02:04 PM
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.
Your answer
Follow this Question
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