Search Unity

2 box collider 2d on 1 object

Discussion in 'Scripting' started by xeonheart, Nov 21, 2018.

Thread Status:
Not open for further replies.
  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    i have 1 character, that has 2 box collider 2d on it, i am not sure how about doing this, but 1 box collider is on its feet, the other is on the head of the character. if the feet hits another character, then do something, if the character gets hit in the head.. then take away life... i currently have something like:

    Code (CSharp):
    1.     public BoxCollider2D box_Head;
    2.     public BoxCollider2D box_Bottom;
    3.  
    4.     void OnCollisionEnter2D(Collision2D collision)
    5.     {
    6.         if (collision.gameObject.tag == "Enemy")
    7.         {
    8.        
    9.         }
    10.      }
    11.  
    but i realize maybe i am missing something or maybe i need a different method?
     
  2. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    got ya thanks LurkingNinjaDev, however if i have 2 Box colliders, how do i set to use a 1 of the 2? as my code above, 1 is for the head, the other is for the body.
     
  3. Really?
    Code (CSharp):
    1. public BoxCollider2D box_Head;
    2.     public BoxCollider2D box_Bottom;
    3.     void OnCollisionEnter2D(Collision2D collision)
    4.     {
    5.         if (collision.gameObject.tag == "Enemy")
    6.         {
    7.              if(collision.collider.name == box_Head.name) {
    8.                   // head collision
    9.              } else {
    10.                  // feet collision
    11.              }
    12.         }
    13.      }
    or something like that.
     
  4. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    unfortunately that didn't work, but still looking into it. i believe you at least gave me the direction, big thank you :) i will see what my small brain can come up with or look online as well. if you figure it out, awesome, but thank you for assisting :)
     
  5. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    ok sadly still stuck on the same problem, i tried to switch instead of using 2 box colliders, use 1... and the head part is edge collider.

    so here is what i have so far:

    Code (CSharp):
    1.     public BoxCollider2D box_Bottom;
    2.     public EdgeCollider2D Line_head;
    3.  
    4.   void OnCollisionEnter2D(Collision2D collision)
    5.     {
    6.         if(collision.gameObject.tag == "Player")
    7.         {
    8.             Debug.Log(Line_head.name);
    9.         }
    10.  
    11. }
    I am trying to figure out, if the player hits the edge line, and NOT the Box collider then do this... just trying to put together the collision parameter with Line_Head which is a EdgeCollider2d object... not sure how :(
     
  6. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    ok so i researched and found this other post, https://answers.unity.com/questions/188775/having-more-than-one-collider-in-a-gameobject.html

    saying cant have 2 box colliders... well you can, but thats the limitations of unity... so i put a edge collider and a box collider... so i just want to know if the edge collider and box collider collide... is this possible with unity? or its limitations once again? i do get the names of the object: Debug.Log(feet.GetComponent<GameObject>().name);

    but i want to get the box collider name or the edge collider, so i can see if they hit.. then do this... else do something else.
     
  7. guavajam

    guavajam

    Joined:
    Nov 24, 2018
    Posts:
    2
    The easy way to get around this would probably be to have some empty GameObjects with each collider you want as a child. That way you can have two box colliders.

    In your script you're only saying that you want to get line_head's name if the Player collides with something. Is the Player even colliding with anything? Aren't both colliders on your player already?
     
  8. BTW you can use only one BoxCollider2D and when you collide, you just ask for the other, compare to enemies and such (layers or tags as you wish) and you can find the contact point through https://docs.unity3d.com/ScriptReference/Collision2D.GetContacts.html
    If the contact point is on the top of your collider it was head, if the point is on the bottom, it's feet.
     
  9. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    cool thank you both, ok, so i now have a game object under the character/players feet and has Tag Player_Feet, so when it touches the enemy, which the enemy has 2 colliders, they are not the same, 1 is a box, the other is an edge... i want the game object that is the child of player if collide with enemy edge collider, then print something, i do have the script attached to the enemy... not sure how to call out from the void OnCollisionEnter2D(Collision2D collision) function??
     
  10. If I understand you correctly you have two separate game objects with colliders. You will need to attach a separate script too to detect collision (with the OnCollisionEnter2D in it).
    Obviously you can then call a method in the other script to handle the stuff on the main game object, but you need the separate script (this is why the one collider with detecting the hit point is much performant, less scripts, less game objects, less collider, less headaches).
     
  11. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    i gotya, thank you lurkingNinjaDev, i thought it was possible to just get or find out which collider was collided with the player and/or enemy, in my case:
    player has box collider and a child game object which also has a box collider for the feet.

    enemy has box collider and an edge collider, and the script. i was hoping to see if using OnCollisionEnter2D, with
    Code (CSharp):
    1.  
    2. public EdgeCollider2D head;
    3. public Transform Player_groundCheck;
    4.  
    i was hoping to write something like, detect collision from Player_groundCheck to Head (again head is the enemy), if true then do something, else do nothing... but if its become more complicated... makes me think of other things now about unity.
     
  12. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    getting very frustrated with unity, OK

    so i have enemy with an empty game object, with box collider 2d, a character/player with an empty game object with a box collider2d, and i tag it with "Player_Feet", and it doesnt work... here is my code below, the script is attached to the enemy empty game object that has the box collider 2d:

    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if(collision.gameObject.tag == "Player_Feet")
    4.         {
    5.             Debug.Log("Player Feet");
    6.         }
    7.     }
     
  13. JumpingJacks

    JumpingJacks

    Joined:
    Sep 19, 2017
    Posts:
    1
    Hi guys, just want to comment here for future use, because I just had to go through this problem.
    Here is an image of my 2 seperate collision boxes on my 1 player gameobject:
    upload_2020-8-3_20-26-40.png

    Here is a snippet of the code I used to test if I can get an component array returned of a specific gameobject and test if the value will get returned from top to bottom. NOTE: Trigger is set to true on the first collider.

    Code (CSharp):
    1.    
    2.         BoxCollider2D[] multibox = gameObject.GetComponents<BoxCollider2D>();
    3.         bool head = multibox[0].isTrigger;
    4.         bool feet = multibox[1].isTrigger;
    5.  
    6.         print(head);
    7.         print(feet);
    And here are the results:

    upload_2020-8-3_20-32-4.png

    This means that it gets the components in chronological order, which is awesome.
     

    Attached Files:

    AL0DIA, JeongMinMoon and hoochiscrazy like this.
  14. Andy_Mai86

    Andy_Mai86

    Joined:
    Aug 1, 2023
    Posts:
    2
    Hi everyone, I get into this post....beyond you can find very.simple code, which doesnt work....

    code:
    void OnCollisionEnter2D(Collision2D col)
    {
    if (col.gameObject.tag =="princess")
    {

    Debug.Log("OnCollisionEnter2D with blondy");

    }
    else
    {
    Debug.Log("no collision to blondy");
    }



    }
    it never get into the if branch (but should, after I checked the tag have Benn setted properly).....I don't know, any suggestion would be appreciated
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    Please create your own threads rather than necroing/hijacking existing ones.

    Also, here's how to post code on the forums.

    No idea what the "branch" is but if neither is logged then it's not being called. If only one is being called then it's because that tag is there or it isn't or isn't the same upper/lower case etc.

    Thanks.
     
Thread Status:
Not open for further replies.