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

Question Check if object collided with other collider

Discussion in 'Scripting' started by elliotbra, Aug 11, 2023.

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    Hi, I have object A and object B. Object A has a Circle collider and a Box collider. Object B has a circle collider. How do I check from object B's script, if object B collided with object A's Box collider? Not the circle collider tho.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Sounds like a potential XY Problem. What is your actual usecase?

    Depending on the setup you could make one of the colliders a trigger instead. Or, if the objects are only ever supposed to collide with one of the colliders, maybe collision layers can be helpful. You could also drag references of both the colliders to the other object and then compare the collision with the collider references. The latter seems like bad architecture tho, unless the objects are very tightly related anyways. If the colliders are on different child objects under one parent, you could also attach separate scripts to those objects, which allows you to figure out which one it is using GetComponent.
    The correct approach probably depends on what you are actually doing here.
     
    Kurt-Dekker likes this.
  3. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    What I'm trying to do, is a magnet, that pulls all the coins to the player, but only when the coins reached the box collider around the player(so that the coins don't come from everywhere)
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    I would then probably turn the outer area into a trigger zone to use OnTriggerEnter and OnTriggerExit to control this behavior.
     
    wideeyenow_unity likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohhh, you mean you have a collider for the player itself, but also have a player detection radius(sphere) that the coins will hit with their collider, and therefor move towards player, hit players collider then activate a plus 1. Gotcha.

    Technically if you want that supposed function, it would be best to have a child object on player, and it have sphere/circle collider, that only if the coin itself sees a collision with that collider it moves towards player center.

    Or, you can just make players position declared as a static Vector2(or 3), and the coin checks if the distance is within a threshold, then move towards player. But without multiple stop checks this method would be performance heavy. So true, the collider would be the easiest and more performant method.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Viable for reasonably low object counts, but the Unity collision and trigger system implement spatial partitioning through octrees (as far as im aware), so it will scale a lot better than having each coin check the distance towards the player each frame. However, for a couple douzen or hundred objects this is probably irrelevant. Still, i think the collider/trigger approach would likely be the best solution.
     
    wideeyenow_unity likes this.
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Yup, that's why I mentioned stop checks. Things like rounding the position values, so it's not constantly checking each frame, which would be a ridiculous cost in performance(in high quantity). But it's just one method I came up with when not wanting to add more colliders in certain cases.

    Another thing I just remembered, is
    OverlapSphere()
    or whatever it's 2D counterpart is, and have that run once a second(or less), if really trying to milk performance(versus a list of known colliders). But true, the less code needs to think or do a bunch of calculations, the better the performance.
     
    Yoreki likes this.