- Home /
pick up item collision
I want the player to pick up an item if pressing a key and colliding with it.
is OnCollisionStay in the player class the way to go?
void OnCollisionStay(Collider other){
if (Input.GetKeyUp(KeyCode.E)){
if (other.tag == "Item"){
Inventory.AddItem(other.thing);
}
}
}
In the player class I would reference the Inventory which has an AddItem() function. would it be that simple?
thing being a GameObject var from the "other" object.
Thanks guys, I'm an artist working in a program$$anonymous$$g vacuum. It helps a lot to just get some feedback
Answer by bodec · Mar 22, 2012 at 09:55 PM
loose a if statement and combine them in one statement if(input.GetKeyUp(keyCode.e)&&other.tag == "Item"){Inventory.AddItem(other.thing);
this will save on some computing also switching to the GetKeyUp is the right route for what you are wanting to accomplish.
Answer by farooqaaa · Mar 23, 2012 at 12:27 AM
The first parameter of OnCollisionStay is Collision not Collider.
void OnCollisionStay(Collision other){
if (Input.GetKeyUp(KeyCode.E)){
if (other.collider.tag == "Item"){
Inventory.AddItem(other.thing);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613065426im_/https://answers.unity.com/themes/thub/images/avi.jpg)