x


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.

more ▼

asked Mar 22 '12 at 09:36 PM

Richard gravatar image

Richard
14 3 5 5

Thanks guys, I'm an artist working in a programming vacuum. It helps a lot to just get some feedback

Mar 27 '12 at 04:27 PM Richard
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 22 '12 at 09:55 PM

bodec gravatar image

bodec
356 6 13 18

(comments are locked)
10|3000 characters needed characters left

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

answered Mar 23 '12 at 12:27 AM

farooqaaa gravatar image

farooqaaa
416 2 5 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x140
x67
x18

asked: Mar 22 '12 at 09:36 PM

Seen: 957 times

Last Updated: Mar 27 '12 at 04:27 PM