- Home /
Trying to pickup an item via trigger, character has 2 collision boxes which are causing it to pickup twice.
I'm sure this question has been asked multiple times but I can't for the life of me word it correctly to find exactly what I need all of them seem to be related to other things.
I have a pickup object in the level with a trigger box. When my character enters the trigger it destroys the object and ++ the variable of the item. But because my player has both a circle and a box collider most of the time it's firing twice. Does anyone have a better way of doing things?
Again sorry if this is an obvious dupe I just can't seem to find what I'm looking for.
Very surprised that something like this isn't common for everyone making a 2d game... Still need help if anyone knows?
Answer by HAndLol · Jan 24, 2020 at 10:13 PM
didnt you try to add a bool?
//add a new bool
bool done = false;
//before anything check for your new bool
private void OnTriggerEnter(Collider other)
{
if(!done){
done = true;
//do your code here
}
}
Seems a bit fiddly if dealing with multiple pickupable objects, or if there's multiple of that one object to collect.
Doing it this way seems to work out, however it seems unusual, must be a better way.
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Gem")
{
if(!pickupDone)
{
pickupDone = true;
Destroy(other.gameObject);
game.gems++;
game.UpdateText();
Invoke("ResetPickup",0.2f);
}
}
}
void ResetPickup()
{
pickupDone = false;
}
i dont know of any other way to do this @Divinitize1 ¯_(ツ)_/¯ thankfully this shouldnt be hard to research!
Your answer