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

Bug Distinguishing head and body collisions

Discussion in 'Scripting' started by Norazalt, Mar 4, 2023.

  1. Norazalt

    Norazalt

    Joined:
    Feb 21, 2023
    Posts:
    2
    Hi everyone, i have a GameObject which is Pig with tag enemy containing 2D box collider, 1 of its child GameObject is weakpoint with weakpoint tag containing 2D edge collider, however when collision occurs between player and weakpoint the console shows edge collider 2D with tag as enemy. I don't know how to fix it. Can anyone help me?
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    Try putting a rigidbody2D on the child. The tag inheriting behavior likely comes from not having one on it.
     
  3. Norazalt

    Norazalt

    Joined:
    Feb 21, 2023
    Posts:
    2
    Thank you. I found this problem, it's because child gameObject always inherits tag from parent gameObject so have to set different layer.
     
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    While that can work, Unity only gives you a limited number of layers to work with. In larger games, each layer can become critical to have for unique purposes and using them in that manner wouldn't be an option. As I said before, having a rigidbody2D on the child object should be all you need and wouldn't require use of another layer. As a quick way to test it, if you set up an example in an empty project with just 3 objects, one of those being the child of another, and give them all rigidbodies and different tags, it will work. I tested it myself just to confirm and it behaved as expected.
     
    Chubzdoomer likes this.