Search Unity

Depending on what collider gets hit, do this...

Discussion in 'Scripting' started by xeonheart, Dec 11, 2018.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    i am trying to figure out a way, i have 2 collider 2d's, 1 is a circle, or actually multiple circles, and 1 is a rectangle, what i would like is:
    IF hit in square with tag == enemy, then do this
    else if hit with any of the 3 circles and tag == enemy, then do this.. how can i find what collider it is using with the
    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.     }
    using this method above? trying to find the right method/variable for the collision parameter :(
     
  2. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Sorry LurkingNinjaDev, maybe i am not explaining it right.

    I have a character, it has a box collider2d and a circle collider2d, and if object hits box collider2d, then do this, if collids
    with circle collider2d, then do this.

    actually i now changed it from 2 circles to 1 edge collider 2d.

    so if objects collides with edge collider, then do this, if collides with box collider2d, do this, i am looking at the documentation and trying it now... and it does show "Player" but, i am hoping it can say... Player.boxCollider or Player.EdgeCollider etc. etc., to show which collider is being used.

    I tried to use collision.collider.name, again only gives me.. "Player"...
     
  3. Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision) {
    2.     if (collision.collider is BoxCollider2D) {
    3.         // do something
    4.     }
    5.    [...]
    6. }
    But why? What is the meaning of this? Usually it is bad to have more than one collider on an object (except for special circumstances).
     
  4. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    hey LurkingNinjaDev,

    big thanks, the reason is i did try to use 1, then of course with the help of this cool post:
    https://answers.unity.com/questions/783377/detect-side-of-collision-in-box-collider-2d.html
    specifically ValderZhu, it sorta works, and doesnt.

    i say it sorta works because when my character or player hits the sides, sometimes it will also show that it hit the top of the enemy character. which i dont want, i just want if it hits left or right, then do this, if it his to top of the enemy character, then do this. i tried and it does work again, but sometimes it wont work and show that it hit both top and bottom.

    the main project i am doing which is helping me to learn is a simple mario clone platform. if player hits the top of the head of the enemy, enemy dead, if he hits the sides... then his health gets taken away.

    so far this is one of the last pieces but most difficult for some reason :(

    so i was thinking of using a child game object and putting it on top of the head of all enemies, but wouldn't that cause performance or more coding? what would you suggest on this? any input would be great :)
     
  5. xeonheart likes this.
  6. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello LurkingNinjaDev, sorry for the late reply, big thank you for pointing me to the right place, after trying it out, it works perfect, BUT, for some strange reason, when my character does get hit, it decrements values by 2, and the weird thing is, I have it to decrement the value by 1... but hey, at least it works :) big thanks again, here is the code to hopefully it helps with anyone, i may post a new thread to help troubleshoot this why it decrements by 2 and not 1.

    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.  
    4. foreach (ContactPoint2D hitPos in collision.contacts)
    5.             {
    6.                 if (hitPos.normal.x == -1.0 || hitPos.normal.x == 1.0)
    7.                 {
    8. Debug.Log("HIT!");
    9. numofHearts -= 1;
    10. }
    11.  
    12. }
    13.  
    14.     }