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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Object with several colliders

Discussion in '2D' started by Dmumzo, Dec 9, 2019.

  1. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Imagine that Object A has two different colliders X and Y.

    How can I make that collider X interacts with another Object B but that collider Y doesnt interact with Object B?
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Depends on what you want to do. Let's say Object A is your player and your player has a collider "X" but he also has another collider for his weapon collider "Y". You want the player you take damage when collider X is hit but not when collider Y is hit.

    On your weapon, add a script and OnTriggerEnter2D or OnCollisionEnter2D you set if Object B hits damage = 0; Which if you have a set damage, lets say Object B deals 35 damage. Then you set + 35 so that no damage was dealt.
     
  3. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Being more specific, my Object A is my player and has a box collider and a circle collider. I want the box collider to collide with platforms only and i want my circle collider to collide with enemies only.

    So my player consists of only one object, not two (like in your example, if I understand it correctly, where my player has two objects: the player itself and his weapon).
     
  4. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Ok, I solved it: On your Object A, you create a child object in which you put the second collider. Then you set different layers for the parent and child objects. Finally you adjust the collision matrix (project settings, physics) to determine what collides with what.
     
    MisterSkitz likes this.