Search Unity

how to distinguish multiple objects in a gameobject with multiple colliders?

Discussion in 'Scripting' started by kalhach, Feb 24, 2021.

  1. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    42
    hi good day again i have a problem with a game that i'm developing... you see the thing is how i could distinguish multiple objects in a gameobject with multiple colliders? i have a game object with two colliders and one is the main collider and the other is the top collider i use the first to generate a boxcast but the second one i use it for normal collisions like oncollisionenter and in the game itself i have many objects with some tags and layermasks and different names this is the image of the object:

    upload_2021-2-23_23-29-46.png
    as you can see it has two different boxcolliders as i named before and the code is this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class SimpleJump : MonoBehaviour
    9. {
    10.     [SerializeField] private LayerMask PlatformlayMask;
    11.     public float jumpvel;
    12.     float jumpboost = 8;
    13.     Rigidbody2D rb;
    14.     private int count = 0;
    15.     bool isground;
    16.     public BoxCollider2D boxcollider2D1;
    17.     public BoxCollider2D boxcollider2D2;
    18.     public ParticleSystem particleExplosion;
    19.     public GameObject player;
    20.  
    21.     void Start()
    22.     {
    23.         rb = gameObject.GetComponent<Rigidbody2D>();
    24.     }
    25.     void Update()
    26.     {
    27.         float exHeightxt = .3f;
    28.  
    29.         if (isground == false)
    30.         {
    31.             count = 0;
    32.         }
    33.         if (isground == true && count == 1)
    34.         {
    35.             count = 0;
    36.         }
    37.         Debug.DrawRay(boxcollider2D1.bounds.center + new Vector3(boxcollider2D1.bounds.extents.x, 0), Vector2.down * (boxcollider2D1.bounds.extents.y + exHeightxt), Color.white);
    38.         Debug.DrawRay(boxcollider2D1.bounds.center - new Vector3(boxcollider2D1.bounds.extents.x, 0), Vector2.down * (boxcollider2D1.bounds.extents.y + exHeightxt), Color.white);
    39.         }
    40.     public void jump()
    41.     {
    42.         if (isgrounded() && count < 1) {
    43.             rb.velocity = Vector2.up * jumpvel;
    44.             count++;
    45.         }
    46.     }
    47.  
    48.     private bool isgrounded()
    49.     {
    50.         float exHeightxt = .3f;
    51.         RaycastHit2D raycasthit1 = Physics2D.BoxCast(boxcollider2D1.bounds.center,boxcollider2D1.bounds.size, 0f, Vector2.down, exHeightxt, PlatformlayMask);
    52.         if(raycasthit1.collider != null)
    53.         {
    54.             Debug.Log("hitfloor");
    55.         }
    56.        
    57.         return raycasthit1.collider != null;
    58.     }
    59.     void OnCollisionEnter2D(Collision2D col)
    60.     {
    61.         if(col. == "obstacle")
    62.         {
    63.             player.SetActive(false);
    64.         }
    65.         if (col.gameObject.tag == "coin")
    66.         {
    67.  
    68.         }
    69.         if(col.gameObject.tag == "jumpboost")
    70.         {
    71.             rb.velocity = Vector2.up * jumpboost;
    72.         }
    73.         if(col.gameObject.tag == "antigrav")
    74.         {
    75.             rb.AddForce(Physics2D.gravity * -1);
    76.         }
    77.         if (col.gameObject.tag == "normalgrav")
    78.         {
    79.             rb.AddForce(Physics2D.gravity * -1);
    80.         }
    81.     }
    you can notice in the oncollisionenter2D method that the first two ifs are incomplete because of the doubt that i have and that would be all i hope you can help thanks in advance
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You can't. Put the other box collider on a child object.
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    your main collider and top collider are under the same Tag, It's mean what ever top collider can interacting to so does the main collider. on top of that if a large collider cover both main part and top part, OnCollisionEnter2D or Exit will trigger twice.

    you only have one option, create a empty child object with collider and Mono script under difference tag
     
  4. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    42
    much appreciated for your help, i will do that and check the colliders both of you thanks again for your help :)