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

Not sur about how collider work

Discussion in '2D' started by shuskry, Nov 23, 2020.

  1. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    Hello everybody :)

    I'm trying to launch projectil on a player object. But my player have two type of collider.
    -Collider for move system ect...
    -Collider for projectil.

    I need two type of collider because the movement collider is set only on the player's feet.
    But the projectil collider take all the player size.

    I have a Root object wich have a Rigidbody2D, a box collider2D, a script to triggerEnter collision, and set on layer "Player".
    In this object I have a child , set on layer "Collider type2", with a box collider2D.

    In physics 2D in preference, I have set only collision to "player/wall" and "collidertype2/projectil"
    And for finish, my projectil object set to layer"projectil" with a script to move and trigger collision

    The problem appear here, when the projectil touch the player, the script on root object with "Player" layer is triggering the collision ...

    there is something that I missed about collider ? Can I have 2 Type of collider in a prefab GameObject?

    Thanks for your help :)
    Have a nice day!
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    You can have multiple kinds of Colliders on a GameObject. Why are you putting two colliders on the Player? You said for the move system and for the projectile. But, I dont understand why there is a Projectile collider on the player, shouldnt that be on the Projectile itself? You can make the movement collider be the entire player's size instead of just the feet, it should still move the same.

    Anyways, my guess is that the Root object's Collider is larger than the actual collider on the child object and it is only coming into contact with that one.

    My suggestion is try to get it down to Colliders on just on gameobject, either the Root or the Child, but not both. And if you can make it just one kind, aka BoxCollider2D or something, the better. You can alter different ways of triggering by checking tags, layers, gameobject names, etc.
     
    shuskry likes this.
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Any collider on an object, children included will call OnTriggerEnter2D() on every script attached to the object children included. No matter how the object is setup, it is treated as a compound object.

    You are in the right direction with having different layers for the interactions. I usually have a "Solid" layer where only solid collides with solid. I apply this to colliders on the player and any solid object that will stop the player. None of these are set as triggers. I will use OnTriggerEnter2D() and then use tags/layers/names to discern what caused the trigger, pickups, projectiles or any other trigger interaction.

    This is fine if you want a solid collider around the whole player, but for most of what I have done I only want the feet to be solid with 2D.

    This is not necessary, both 2D and 3D physics treats the colliders as a compound collider no matter how its setup no matter how many child objects have a collider or colliders. If there is a some sort of performance issue I haven't seen it, and I use the profiler all the time to see what's taking the most time.
     
    shuskry likes this.
  4. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    My bad...

    I was calling " OnTriggerEnter" instead of "OnTriggerEnter2D"...
    Everythings work like a charm now :D

    Thanks for your help !