Search Unity

Collier detecting and giving feedback for multiple items?

Discussion in '2D' started by pandaplop25, Sep 12, 2019 at 3:59 AM.

  1. pandaplop25

    pandaplop25

    Joined:
    Oct 8, 2018
    Posts:
    2
    I am quite new to C# and my code may make some cringe so sorry if it upsets.
    Code (CSharp):
    1.  
    2. void OnCollisionStay2D(Collision2D other)
    3.     {
    4.      
    5.  
    6.         if (other.collider.gameObject.layer == LayerMask.NameToLayer("Item") && Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
    7.         {
    8.             GameObject itemPickedUp = other.gameObject;
    9.             PlayerItem item = itemPickedUp.GetComponent<PlayerItem>();
    10.             AddItem(itemPickedUp, item.ID, item.Amount, item.Bonus, item.Rune, item.RuneAmount);
    11.         }
    12.      
    13.  
    14.     }
    15.  
    This code is suposed to add an item to the player invintory when it detects an item with the layer name "Item" while the player presses shift. It works alongside other code that is not important or visable in this section. This code works perfectly fine if the player is standing on only one object but if the player stands on 2 objects on the same spot then it will pick up both at once. Is there any way single off the highest priority item so I can make it only pick the top item up?
     
    Last edited: Sep 12, 2019 at 4:05 AM
  2. BeMercy

    BeMercy

    Joined:
    Mar 20, 2019
    Posts:
    2
    idk how its your code but if the 2 objects have the same layer, i think that maaay be u can do a foreach structure to get all the items that is under u. then u can check the first item and just add that in inventory
    pd: idk if it's will works :)
     
  3. pandaplop25

    pandaplop25

    Joined:
    Oct 8, 2018
    Posts:
    2
    Another example of my problem is if i do
    Code (CSharp):
    1.  
    2. void OnCollisionEnter2D(Collision2D other)
    3.         {
    4.             int colidingID = other.gameObject.GetComponent<ItemInfo>().ID;
    5.  
    6.             Debug.Log("ID-" + colidingID);
    7.  
    8.         }
    9.  
    To make it log the ID of the item I touch when I touch it, then it will log the ID of every item in the stack all at once. I am trying to find out if there is a way to make it only log the highest priority item. (The item that shows up on the top of the stack)
     
    Last edited: Sep 12, 2019 at 5:09 AM