For Some reason, collider check has extra collider.

I am making a small prototype of a Unity 2D game. I have 2 colliders for my character. This helps me because I want my character to be able to wall jump. This means that when I collide with the wall, my collider detection script assumes that I am hitting 2 colliders, and helps me customize animations.

            //on the ground, change y position as necessary, and if you are pressing up, held = true
            if (InAirDetection.AirDetect == 1)
            {

                position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
                if (Input.GetButton("Vertical") == true)
                {
                    held = true;
                }
                position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
            }

            //on the wall, change y position as neccessary, and if you are pressing up, held = true
            if (InAirDetection.AirDetect == 2)
            {
     
                position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
                if (Input.GetButton("Vertical") == true)
                {
                    held = true;
                }
            }


            //in the air, if held is true, change y. If you aren't pressing up, held is false.
            if (InAirDetection.AirDetect == 0)
            {
                if (held == true)
                {
                    position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
                }
                if (Input.GetButton("Vertical") == false)
                {
                    held = false;
                }
            }

        }
        // apply the transformations
        transform.position = position;

This is some of my code.
I tried to make it so that if I let go of up in the air and pressed it again and nothing would happen.
It works but there is a slight problem…

By slamming one collider(head) against the different collider(ceiling), the head goes into the body collider. This makes the collider detection thinks there is always one collider touching and that I am holding up. That means that I can jump about 5 times the intended height. Another side effect is that sometimes there seems to be a force acting on one the side of the object, like wind. This doesn’t always happen.

How can I remove the bug completely? Is my only option to make the character have 1 collider?

I had an extra line of code. Also, I changed the collider’s position a tad bit. This was the extra line:

position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;

Now, however, if I hold it and jump(or jump, let go, and hit up again before hitting the ground), I jump an extra 1.8/1.9 units higher.

Dude, you are using Update to work with rigidbodies (bad) while also affecting their position directly (bad) using transform.position instead of rigidbody.position(worse) and you have two colliders that are not supposed to interact on a layer with self-interaction enabled (also bad)

Of course you’re going to get bugs!


Repeat after me

  • Transform.position for moving a physics-less objects
  • Rigidbody.position for teleportating a rigidbody
  • Rigidbody.MovePosition for moving a kinematic rigidbody
  • Rigidbody.velocity or Rigidbody.AddForce for moving a dynamic rigidbody

To more directly answer your question, if you want to put multiple colliders on an object but you don’t want them to collide with eachother, you can put them in a Layer together, then in the project settings under “Physics” you can disable the layer from colliding with itself in the collision matrix