How do I manage multiple colliders on a gameObject?

Hey all, first time poster, I don’t know how formatting works but the preview box isn’t giving me high hopes. I hope I posted this to the right section!


I’m not super new to Unity, I’ve been learning for maybe 6 months now.


I’m working on making my own first simple project,


Some preface information:
I have been experimenting with this for a short time now (3-4 coding sessions) and originally I had 3 boxcollider2D colliders on the parent gameObject, and it was janky and I couldn’t differentiate between which collider was where (without clicking on the collider) so to make things more organized I made a child object for each collider and referenced them in script using [SerializeField]


The problem:
I have a script that handles all player movement (basically a movement controller) and it references 3 child gameobjects that have their own hitboxes. I’ve been using debuglogs to make sure the collisions work as I need them to and things are going well for the most part.

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

public class PlayerMovement : MonoBehaviour
{
    /*
     
         Comments for things currently working in my code:
         Forward and Backward Explosions.
         Forward Input. 
         Backward input.
         Jumping.

         Comments for the things currently not working in my code:
         Air movement.
         Setting player upright if not landing properly
    */
    [Header("Player Movement")]
    [SerializeField] float playerSpeed = 2f;
    [SerializeField] float jumpHeight = 2f;
    [SerializeField] float torqueSpeed = 2f;
    //
    [Header("Automated Movement")]
   // [SerializeField] float jumpSpeed = 2f;
   // [SerializeField] int pogoHeight;
    //
    [Header("Landmine Paramaters")]
    [SerializeField] float explosiveForceX = 3f;
    [SerializeField] float explosiveForceY = 3f;
    [SerializeField] float negExplosiveForceX = -3f;
    [SerializeField] float negExplosiveForceY = -3f;

    [Header("Colliders")]
    [SerializeField] BoxCollider2D headCollider;
    [SerializeField] BoxCollider2D bodyCollider;
    [SerializeField] BoxCollider2D pogoCol;
    //
    Rigidbody2D rb2d;
    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        pogoCol = GetComponent<BoxCollider2D>();
        headCollider = GetComponent<BoxCollider2D>();
        bodyCollider = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        GroundMovementHandler();
        AirMovementHandler();
        PogoMovementHandler();
    }
    void OnTriggerEnter2D(Collider2D collider2D)
    {
        if (collider2D.gameObject.tag == "ForwardExplosive")
        {
            ExplodeForward();
        }
        else if (collider2D.gameObject.tag == "BackwardExplosive")
        {
            ExplodeBackward();
        }
    }
    private void ExplodeForward()
    {
        Vector2 explosion = new Vector2(explosiveForceX, explosiveForceY);
        rb2d.AddForce(explosion, ForceMode2D.Impulse);
        rb2d.freezeRotation = true;
        Debug.Log("I am touching a forward explosive");
    }
    private void ExplodeBackward()
    {
        Vector2 explosion = new Vector2(negExplosiveForceX, negExplosiveForceY);
        rb2d.AddForce(explosion, ForceMode2D.Impulse);
        rb2d.freezeRotation = true;
        Debug.Log("I am touching a backward explosive");
    }
    void JumpHandler()
    {
            // Vertical = up and down

            // Move up and down
            float vertitalMovement = Input.GetAxisRaw("Vertical") * jumpHeight;
            vertitalMovement *= Time.deltaTime;
            transform.Translate(0, vertitalMovement, 0);
    }
    void GroundMovementHandler()
    {
            // Horizontal = left and right

            // Move left and right
            float horizontalMovement = Input.GetAxisRaw("Horizontal") * playerSpeed;
            horizontalMovement *= Time.deltaTime;
            transform.Translate(horizontalMovement, 0, 0);
            JumpHandler();
    }
    void AirMovementHandler()
    {
        if (pogoCol.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
   oid PogoMovementHandler()
    {
        //Debug.Log("Jump down and up by yourself");
        if (pogoCol.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            Debug.Log("Jump up and down by yourself");
          //  transform.Translate(Vector2.up);
        }
        else if (!pogoCol.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }
    }
}

The problem I am currently having with colliders is “pogoCol”, in the method “EnableAirMovement()”
unity is saying that while my collider is touching the ground (my player starts in a grounded position) Unity is saying that pogoCol (the collider set to touch the ground) is not actually touching the ground.


I have moved the colliders into the sprite a little so it is visually touching the ground and my Debug.Log for “Air movement enabled” is updating every frame to tell me that my collider is not working as I intended.


I’m not sure what other information I can really add to this post, my head isn’t clear as it is very late at night and I feel like a bit of a zombie, if anyone can manage to make sense of this then your input is much appreciated.


I’m off to sleep and I will continue to work on a solution in the mean time and I will answer any and all relevant questions if there are gaps to fill.

:).

I ended up fixing the issue.

I used 3 different colliders all attached to the parent (one box2d, capsule2d and a circle2d)