0
\$\begingroup\$

I want the player to be able to grab an object (in this case the knife) when he is in the object's trigger zone. He can get the knife but there is a problem: if I press grab button outside of the trigger zone, then he can get the knife immediately after entering the zone. I want Unity to get the input only when the player is in the trigger, otherwise it should not be working.

Here's the grab script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grab : MonoBehaviour
{
    public GameObject player;
    private bool grabbed;
    public GameObject knifePosition;
    private bool isInTrigger;

    void Update()
    {
        if (Input.GetButtonDown("Grab"))
        {
            grabbed = true;

        } else
        {
            grabbed = false;
        }

       if (grabbed && isInTrigger)
        {
            gameObject.transform.position = knifePosition.transform.position;
       }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject == player && grabbed)
        {
            gameObject.transform.SetParent(player.transform);
            isInTrigger = true;
            Debug.Log("Picked up");
        }
    }
}

enter image description here

\$\endgroup\$
0
2
\$\begingroup\$

You do not set your grabbed back to false in case you do not press the button. This causes your script to remember the pressing and will pick up your item as soon as you enter the zone.

void Update() {
    if (Input.GetButtonDown("Grab"))
    {
        grabbed = true;
    } else {
        grabbed = false;
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Thanks for your answer. This solved that problem. But it seems like now I can't get the object. I updated the code and added a line to transform the object when the player gets it. I don't why but the knife is not a child of the player when I press the button. \$\endgroup\$
    – Ali
    Mar 7 '21 at 20:17
  • \$\begingroup\$ Well, on which GameObject is the Grab script? Since that is the one that gets under parent. If your knife has only knifePosition as reference, use that one for setting the knife under player. \$\endgroup\$
    – Zibelas
    Mar 7 '21 at 21:09
  • \$\begingroup\$ The Grab script is on the knife game object. Knife also has the player game object as reference in the inspector. \$\endgroup\$
    – Ali
    Mar 8 '21 at 3:54
  • \$\begingroup\$ But you do not compare in your collider against the reference of player but an instance of the player. Not sure how your Player looks like or if your condition is triggered. Add a Debug.Log("Picked up"); in the condition of OnTriggerEnter2D and see if it gets called. If not, you might want to replace the Player.Instance with player. \$\endgroup\$
    – Zibelas
    Mar 8 '21 at 6:45
  • \$\begingroup\$ I replaced Player.Instance with player and added the Debug.Log in the if statement but didn't get any messages in the console. I removed && grabbed from if (collision.gameObject == player && grabbed) and surprisingly it worked and I saw the message. But the problem is that I want to get the input and then collect the object but without && grabbed the player just gets the knife after entering the trigger. I'm not sure but maybe there is something wrong with those two Booleans. \$\endgroup\$
    – Ali
    Mar 8 '21 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .