Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How To Determine the Specific Collider Touching Another Object

Discussion in '2D' started by CodeUnit07, Aug 14, 2023.

  1. CodeUnit07

    CodeUnit07

    Joined:
    Aug 10, 2023
    Posts:
    11
    Hey all,

    I'm working on a 2D project that includes a ball that is supposed to move around the screen bouncing off of object colliders. When a collision happens I want to know what side of the ball made the collision so I can determine the next path for the ball to deflect to. My idea was to add 4 trigger colliders on each side of my ball as attached.

    Screenshot 2023-08-13 192822.png

    My question is that when a collision occurs, how do I determine which collider on the ball is touching the object that the ball collided into(wall or in-game obstacle).

    Also you could let me know if there is another completely different idea that would work better and achieve the same thing.

    Thanks so much!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    A collision callback passes you all the collision information so use that. Look at the contact point(s) and colliision normal(s) for the ball. https://docs.unity3d.com/ScriptReference/Collision2D.html

    Adding these 4 triggers isn't required at all but if you do, the answer is still the same, the collider is provided in the callback.

    This should be pretty obvious but if you've just started then I would recommend by going through some basic physics tutorials rather than asking on the forum; they'll provide you with this detail.
     
  3. CodeUnit07

    CodeUnit07

    Joined:
    Aug 10, 2023
    Posts:
    11
    Yes, I am pretty new to this Unity stuff and maybe it's because I'm not good at formulating questions yet, but I am having trouble finding tutorials or answers to my questions that I'm having. I made this post as a last resort.

    I thank you for your help but I'm still a little foggy. I removed the triggers as you recommended and I'm focusing on using collider callback. I'm trying to use "collision.GetContact" because apparently "collision.contact" isn't a good idea. Using "collision.GetContact(0);" what format will I receive the return? Right now I'm just getting "object" so I think something is wrong.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I don't know what you mean by "format" but I suspect you mean what "type" so you can use the docs I linked previously to answer that question:

    If you look at the docs for GetContact you'll see it returns a ContactPoint2D which contains the point it hit, the collision normal, the collider etc.

    You won't get an answer to your specific question but you will learn what contacts are and what they represent.

    You can obviously use the point to determine where it hit.
     
  5. CodeUnit07

    CodeUnit07

    Joined:
    Aug 10, 2023
    Posts:
    11
    Thank you so much for your help and I apologize for my slowness but after implementing what I thought you said I'm not getting a ContactPoint2D value. What's wrong with my code?

    Code (CSharp):
    1. public ContactPoint2D ballContact;
    2.  
    3.     private void OnCollisionEnter2D(Collision2D collision)
    4.     {
    5.         ballContact = collision.GetContact(0);
    6.         Debug.Log(ballContact);
    7.     }
    For some reason the console is just saying "object" when a collision occurs.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    The docs show you the type ContactPoint2D. If you were to look at it, you'd see a point (Vector2) property as I mentioned above.

    You need to learn how to use C#. These forums are not appropriate for that as they assume a basic working knowledge of it.
     
  7. CodeUnit07

    CodeUnit07

    Joined:
    Aug 10, 2023
    Posts:
    11
    I've been looking at the docs but they don't have any examples of how to write or what the return is for ContactPoint2D.

    Sorry, sorry, I've been trying to learn C# but I'm definitely not very comfortable with it yet. I do have some coding background but I haven't mastered all the special methods and quirks of Unity yet. I'll try to lay off of cluttering the forums until I get it.

    Thanks for your help!
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You don't really need an example on how to access the point property; the docs state it's the point of contact so just access it.

    Regardless, here's how to do it.
    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2. {
    3.     var ballContact = collision.GetContact(0);
    4.     Debug.Log(ballContact.point);
    5. }
    Note that some colliders produce have more than a single contact point.