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

How to prevent Child Colliders effecting the parent collider script

Discussion in 'Editor & General Support' started by ToastHatter, Jul 26, 2017.

  1. ToastHatter

    ToastHatter

    Joined:
    Nov 11, 2016
    Posts:
    53
    I was wondering if there was some way that you can prevent the child's colliders from interfering with the parents colliders and scripts. I'm currently making a 2d platformed and for the attack hitbox it's a child of the player, and it becomes active when i press a button. I keep getting problems where if I attack an enemy using it they will still damage me through my attack's hitbox. How do i prevent this?
     
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    901
    Collisions and triggers get tricky and you need to do some juggling. Without a clear understanding of the setup you have, I can't say what I'd do. However, here are a few pointers:

    1) Use IgnoreCollision. You can disable a collision for a specific event and reenable it after that is finished.
    2) Swap layers to a non-reactive layer. As IgnoreCollision, you can move an object into a layer that isn't active for a collider, and then move it back after the test.
    3) Use objects that aren't actual child objects but which follow a GO and communicate with scripts. In simplified form, create an object 'HitBox' and give it a script something like

    Code (CSharp):
    1. public PlayerScript player;        // set in inspector
    2. public Transform playerTrans;      // set in inspector
    3. Transform _cachedTrans;
    4.  
    5. void Start{
    6.     _cachedTrans = transform;
    7. }
    8.  
    9. void LateUpdate{
    10.     _cachedTrans.position = playerTrans.position;
    11. }
    12.  
    13. void OnCollisionEnter2D(Collision2D col){
    14.     playerScript.hasCollided(col); // do all your collision processing here as if OnColEnter2D was on the script
    15. }
    4) Use Tags/Layers and test in the collision if the collider has a tag you want to respond to.

    I've had plenty of trouble with a trigger on a laser beam to autoshoot when something's in range, but not wanting it to active OnTriggerEvents. In the end the Trigger code tests if the object activating it is a rock, ship or shot. Otherwise it'll ignore the trigger.
     
  3. propipeproductions

    propipeproductions

    Joined:
    Apr 11, 2022
    Posts:
    3
    This helped me out so much. Thanks for the in depth response all those years ago! I had not considered separating my player's detector from the player, then just writing a script to follow the movements. Now my tags work correctly without the parent object adding an additional tag.
     
  4. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    901
    Glad that helped! I've actually moved on from this now, and just do 2Dphysics tests independently without using the built in callbacks. It's soooooo much simpler! I think Unity's use of layers and tags outdated and inflexible and basically more trouble than they are worth. Sadly Unity is tied to a lot of fundaments going back to when it was creating limited games with limited gameplay on limited hardware, and these ideas don't extend well to the modern gaming landscape.