Search Unity

Enter multiple times in onTriggerEnter()

Discussion in 'Scripting' started by fdojruiz, Jun 30, 2022.

  1. fdojruiz

    fdojruiz

    Joined:
    May 9, 2022
    Posts:
    8
    Hello.

    I have a problem with the onTriggerEnter.

    I have a GameObject (Player) with a boxCollider, which has a child GameObject (Weapon) that also has a BoxCollider but with isTrigger marked and the tag "Attack".

    I also have a GameObject (Enemy) with a boxCollider, which has a child GameObject (weapon) that also has a BoxCollider but with isTrigger checked. This GameObject (Enemy) has a script with the OnTriggerEnter method to control when the Player's Weapon collides with it.

    The problem is when the Player's Weapon collides with the Enemy and his Weapon, it is entering the OnTriggerEnter twice, and is counting double the damage.

    Anyone knows how to solve this?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    It shouldn't really be doing that, colliders with isTrigger enabled won't trigger on each other, only on objects which don't.

    It might be caused by having multiple colliders on a single object though, I know Unity does some weird stuff called Compound Colliders which kind of consolidates your colliders into one thing, so you might want to look into that. If that were the case, I might suggest not parenting the weapons to the Player/Enemy GameObjects.

    Edit: For the above solution, it might better to add a separate RigidBody to your weapon to disable the compounding of the colliders instead of just un-parenting it, as it should theoretically unpair them.
     
    Last edited: Jun 30, 2022
    rc82, Zekrom1223 and fdojruiz like this.
  3. fdojruiz

    fdojruiz

    Joined:
    May 9, 2022
    Posts:
    8
    Thanks for the help. It has been solved by adding a RigidBody as you have told me. :)
     
    Zekrom1223 likes this.