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

a begginer in need of help.

Discussion in 'Scripting' started by SAMM1T_, Sep 5, 2021.

  1. SAMM1T_

    SAMM1T_

    Joined:
    Aug 13, 2021
    Posts:
    10
    Hi all, So I am trying to make the player able to pick up items and then those items appear in the players inventory. And so far it works but all the player has to do is simply walk over the item and then it gets picked up. But I would like the player to have to press F in order to pick up any items.

    My code that works for the player to pick up without pressing any key:

    Code (CSharp):
    1.  
    2. void OnTriggerEnter(Collider other)
    3.    {
    4.         if(other.tag == "Item")
    5.         {
    6.             //the item should get picked up.
    7.         }
    8.     }
    9.  
    my code when I try to implement a key needing to be pressed:

    Code (CSharp):
    1.  
    2. void OnTriggerEnter(Collider other)
    3.     {
    4.         if(other.tag == "Item" && Input.GetKeyDown(KeyCode.F))
    5.         {
    6.             //item shoud get picked up
    7.         }
    8.     }
    9.  
    when I use this nothing happens. No error, Nothing gets picked up.
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    You're along the right lines, just a few problems with what you're doing.

    OnTriggerEnter() is only called once when you first enter a trigger. It doesn't keep getting called while you're in there. As a result, unless you enter the trigger and KeyDown at the same time, this will not work.

    One way to implement the kind of pickups you want would be to seperate the collider entry and the pick up control.

    For the collider, when you enter the collider record the transform that you just entered. When you exit the collider or enter a different one, record the change and store either nothing or the new transform accordingly.

    For the input, in Update() when you hit the relevant key, check to see if your stored transform a) exists and b) has the Item tag. If it does, pick it up. If not, don't.

    I'm sure there are other approaches too. There is an OnTriggerStay() for example, that constantly checks whether you're inside a collider, but I tend to avoid that if I can get away with just using enter and exit to record the collisions.
     
    Last edited: Sep 5, 2021
  3. chubshobe

    chubshobe

    Joined:
    Jun 20, 2015
    Posts:
    52
    Code (CSharp):
    1.  
    2. public class Example : MonoBehaviour
    3. {
    4.     bool isPickupKeyDown = false;
    5.     KeyCode pickupKey = KeyCode.F;
    6.  
    7.     void OnTriggerStay(Collider other)
    8.     {
    9.         if (isPickupKeyDown && IsItem(other)) PickUp(other);
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         isPickupKeyDown = Input.GetKeyDown(pickupKey);
    15.     }
    16.  
    17.     bool IsItem(T obj) where T : GameObject, Component => obj.CompareTag("Item");
    18.  
    19.     void Pickup(Collider item)
    20.     {
    21.         // Pickup code
    22.     }
    23. }
    24.  
     
  4. SAMM1T_

    SAMM1T_

    Joined:
    Aug 13, 2021
    Posts:
    10
    thanks everyone for the info, solved by using OnTriggerStay()