Collectable item

I have a few question as I am not very sure, I want my player to collect items from the ground and store it into the inventory.
My first question is:

  1. I have 20 different items, do i need a collider on each of them in order the player to collect it.
  2. The collecting of item do I need to write a separate script an attach to each object or can I just script it together with my playercontroller script.
  3. Is it better to use OnTriggerEnter or OnCollisionenter to collect the item.

It is a simple thing but just need a guidance.

Thank you.

well, you totally need a collider, you can either write code in the character script, or you can write individual scripts. OnTriggerEnter or OnCollisionEnter would work fine for what you are using, unless you use the character to do it, then you need to use OnCollisionEnter.

Thanks for prompt answer…
I will work on this now, I will create a separate script an attach to each of my object.
I will use OnCollisionEnter since i need my player to collect the item. Do I use a tag like the following code:

function OnCollisionEnter(collision: Collision)
{
	if(collision.gameObject.tag == "inventory") //find the object tag with inventory
      //or do I tag my "Player"
    {
         //if yes remove from scene and add to inventory
         Debug.Log("Destroy: " + collision.gameObject.name);
    }
}

Please advice. Thanks.

it looks fine to me, but i probably would go with the player tag

CharacterControllers trigger OnTriggerEnter() just fine, last I checked.

well, I was not aware of that.

When I have the OnCollisionEnter script attach to my object, whenever my player and the item collides it should run the script, right?
Do I need to add anything else to make this happen?

Hi,
OnCollision Enter Script will work absolutely fine . Anything else will depend on your logic. You are on the right path.

what saurabh said

My guess is this all depends on what you are picking up. Using OnCollisionEnter is great and all, but the simple fact of the matter is that if you have entered a collision, you have affected the collision of those objects which have entered it. Using OnTriggerEnter would be better for things like power-ups as a character in motion would not have to stop his motion to pick up an item.

Vice-versa, if you specifically wanted the motion of the player to stop or be affected by what he is picking up then OnTriggerEnter would be the wrong way to go. So if you had a chair and you wanted you guy to pick it up, he would have to strike it, stop, pick it up and then use it. However, using OnCollisionEnter in this respect is probably not the best way to go. You would want to use a keystroke and a ray or dot vector to start the pickup process.

Oh, on a further note, objects that are instantiated within a trigger zone or who’s trigger zone is within the affected objects do not cause the trigger event.

Thanks for the wonderful explanation BigMisterB…
I notice the OnCollisionEnter was not working for me because my object did not have a Rigidbody. But when I added it and remove the gravity, what happens is my player was like kicking the object instead of a collision taking place.
I taught by having CharacterController on my player is enough for a collision to take place with the object.
What am I not getting right here…