system
1
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.
2 Answers
2
bodec
2
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.
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);
}
}
}