Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Loot objects without being blocked

Discussion in '2D' started by Morgomir, Jan 10, 2019.

  1. Morgomir

    Morgomir

    Joined:
    Aug 17, 2018
    Posts:
    9
    Hello, I'm having trouble making a right system to loot items on the ground. I've currently made a bow that can throw arrows and I would like to loot the arrows which are stuck in the walls by walking on it.
    To avoid the other objects to collide the "stuck arrows" I've set the layer to a custom one that will not collide with other objects except the player (using the layer matrix). When the player collides with a loot tagged object it will destroy it (and will truly loot it in the future).

    The problem I face is that when the player collides that item, it stops the player, which is really annoying when looting a lot of stuff.. I've searched some solutions about layers but the thing I'm looking for is a way to make an object colliding with others but not affecting their movement.

    Sorry if the answer looks obvious to you I'm still discovering the features and mechanics of unity and I would be very thankful if you could give me a clue on that. Thanks in advance

    Here is the part of my arrow script :

    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.collider.CompareTag("Walls"))
    4.         {
    5.             rb.isKinematic = true; // Stops the arrow from its movement and freeze it until it's looted or destroyed
    6.             rb.velocity = Vector2.zero;
    7.             rb.freezeRotation = true;
    8.             gameObject.layer = 8; // Loot layer -> will only collide with the player
    9.             gameObject.tag = "Loot";
    10.             Destroy(gameObject, 20f); // Destroy it if the player does not loot it in 20s
    11.         }
    12.     }
    And the part of my player script (which is very simple):

    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.collider.CompareTag("Loot"))
    4.         {
    5.             Destroy(collision.gameObject);
    6.             // + add to inventory
    7.         }
    8.     }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Morgomir likes this.
  3. Morgomir

    Morgomir

    Joined:
    Aug 17, 2018
    Posts:
    9
    Works perfectly, thanks !
    I thought trigger was used for things that needs to be activated only once. As I expected the answer was pretty obvious x)