2
votes

I'm currently attempting to create a 2D game in Unity, and my Player has a box collider on his top half and a circle collider on his bottom half; this smoothens out ramps and stuff.

When I collect a gem, my OnTriggerEnter function calls twice because of both of my colliders, unless I'm falling onto it from above or jumping up to it from below, as one collider clearly touches it before the other.

Is there a way to combine the colliders into one check so that they do not overlap?

The box and circle colliders

The OnTriggerEnter2D function

1
If you add some reference code will be helpful to check quick.Mdyahiya

1 Answers

0
votes

Assuming you don't want to make sizes of Box and Circle collider different. Add a script to Gem having a variable "isUsed" and when the collision occurs set it to true. So that in next time if collision occurs, it can be neglected.

void OnTriggerEnter2D(Collision2D other)
{
    if(other.gameObject.CompareTag("gem")){
          GemScript gemScript = other.gameObject.GetComponent<GemScript>();
          if(!gemScript.isUsed){
           DO YOU STUFF HERE
           gemScript.isUsed = true;
          }
    }
}