Player Object 2 colliders decision issue

Hello, So I have a player object, and the player object moves straight, However I have another object that Instantiates every 5-10 seconds, this is just a wall, and the wall has an opening for the player to maneuver in to avoid getting damaged/hit. the Wall has a parent, which is the entire wall that has the box collider “Collision”, and a child object, that has the collider “Opening”, the hard part is, finding the right, IF else statement, to IF the player collides with BOTH opening AND collision, then that’s a hit, and no points… BUT if it collides with ONLY the opening, then reward a point… again, trying to find the right combo… is difficult, I have the code below, any ideas, thoughts, examples help, I would greatly appreciate it.

public void OnTriggerEnter(Collider other)
{
    // Check if the object hit Collider A
    if (other.gameObject.name == "Square_Colider") // this is the Collider block wall player avoids
    {
        collidingDamage = true;
    }

    // Check if the object hit Collider B
    if (other.gameObject.name == "Square_Opening") // this is the opening box collider player needs to go through
    {
        opening = true;
        //Debug.Log("Entered Collider Opening");

        //// If only Collider B is triggered, score a point
        if (!collidingDamage)
        {
            MainG.AddScore(); // public static Int to add score IF avoided the block wall
        }
    }

    // If both A and B are hit, apply damage
    if (collidingDamage && opening)
    {
        StartCoroutine(Shaking());
    }
    if (!collidingDamage)
    {
        if (opening)
        {
            MainG.AddScore();
        }
    }
//This is trying to reset... but doesnt seem to work.
    private void OnTriggerExit(Collider other)
    {
        // Reset when exiting Collider A
        if (other.gameObject.name == "Square_Colider(Clone)")
        {
            collidingDamage = false;
            Debug.Log("Exited Collider A");
        }

        // Reset when exiting Collider B
        if (other.gameObject.name == "Square_Opening")
        {
            opening = false;
            Debug.Log("Exited Collider B");
        }
    }

here is an example of the wall coming, But has an opening:

Why not just use four colliders for the wall set up so that they have a gap the size of the opening in there and then put the opening collider in that gap? That way you don’t have use a more complicated code setup? You’re not going to have to deal with any meaningful overhead by using a total of five box colliders.

edit: additionally: you make the collider the player is meant to hit smaller than the opening itself, enough that the player will only meaningfully collide with it if they’re relatively close the center. Player perception of accuracy is generally more important than true accuracy.

1 Like

cool thanks for your response Murgilod… so when you say, “four Colliders”, which colliders? and the opening game, I am using a Box collider, but its attached to the child object, which the parent is the wall, with all those colliders to make the gap… but you are saying colliders, which ones, i definitely would like to know, because i agree, anything that can make it easy for me would be great.

For a wall with a gap in it, generally an easier way to handle it is like this. Blender because I don’t want to launch Unity and start a new scene right now:

Generally speaking, you do not want a gap to be additive. The gap should always be an absence of a collider unless absolutely necessary. If the goal is to have the player go through that hole to gain points, what you want to do is put a collider within that area, set a little bit to the rear so that the player won’t collide with both.

This way, if the player collides with any of the box colliders creating the wall, you can just disable the object that would grant points and use that collision data to apply the point penalty.

2 Likes