Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Biggzlar · Dec 12, 2016 at 09:41 PM · c#charactercontrollercollidersontriggerenterontriggerexit

OnTriggerEnter() and OnTriggerExit() called multiple times despite checks.

Yes there are many questions and answers concerning similar topics, but funny enough I have not gotten a satisfying answer out of any of those posts.

The title describes the entire problem I am experiencing, so here is the code. By the life of me I do not understand why this is called multiple times when the collider (isTrigger=true) passes through my CharacterController (maybe the problem lies in this?).

Edit: It has come up several times, so to clarify:

  • The EnemyAgent Script does not use the damageDealt boolean - it is only used to store it as a public variable.

  • There is no other collider apart from the CharacterController attached to the Gameobject in question.

  • Also of note might be that the triggerenter/triggerexit functions are called many, many times and not just twice.

  • Since there are checks in place the single most important question is: Why is the OnTriggerExit() method called prematurely - before leaving the actual collider?

       using UnityEngine;
         using System.Collections;
         using System.Collections.Generic;
         
         public class HitBox : MonoBehaviour
         {
             PlayerHealth playerHealth;
             
             void Awake ()
             {
                 playerHealth = this.gameObject.GetComponent <PlayerHealth> ();
             }
         
             void OnTriggerEnter (Collider other)
             {
                 if(other.gameObject.tag == "EnemyWeapon"){
                     EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
                     if (thisAgent.damageDealt)
                         return;
                     if (thisAgent.attacking) {
                         thisAgent.damageDealt = true;
                         playerHealth.TakeDamage (thisAgent.attackDamage);
                         Debug.Log (thisAgent.ID + " in");
                     }
                 }
             }
         
             void OnTriggerExit(Collider other){
                 if (other.gameObject.tag == "EnemyWeapon") {
                     EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
                     if (thisAgent.damageDealt) {
                         thisAgent.damageDealt = false;
                         Debug.Log (thisAgent.ID + " out");
                     }
                 }
             }
         }
     
    
    

If you are wondering why the specific enemy agent is only sought after the collider is determined - it's done this way in order to have several enemies attack the player at the same time.

Please help...

Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Dec 12, 2016 at 09:43 PM 0
Share

Do the things tagged "EnemyWeapon" have any child objects in their hierarchy with that tag?

avatar image Biggzlar getyour411 · Dec 12, 2016 at 10:07 PM 0
Share

Hi, thanks for your reply. No, for all intends and purposes this is the only object labeled "EnemyWeapon" in each enemy's hierarchy.

avatar image hexagonius Biggzlar · Dec 12, 2016 at 11:22 PM 0
Share

I'd log out col.gameobject.name to see who they are

Show more comments
avatar image Pengocat · Dec 13, 2016 at 01:19 AM 0
Share

Do the character controller have a rigidbody and a child collider?

avatar image Biggzlar Pengocat · Dec 13, 2016 at 07:31 AM 0
Share

Hello and thanks for replying. No, the character being hit only has a CharacterController, it does have a child collider but disabling this one did not change anything about the behaviour of the script.

avatar image roman_sedition · Dec 13, 2016 at 08:30 AM 0
Share

I'm not sure if line 18 and 19 is required, line 20 could be

             if (thisAgent.attacking && !thisAgent.damageDealt) {

I am only guessing that the return on line 19 could cause unexpected behaviour. Or maybe you need continuous collision detection on.

avatar image Biggzlar roman_sedition · Dec 13, 2016 at 10:29 AM 0
Share

Hi Roman, thanks for your advice. Getting rid of the return; and using the check you proposed above did not solve the problem - this was actually how I tried it before.

Since I do not use any rigidbodies, I have no way of using continous collision detection, or does the CharacterController support this as well?

avatar image Hellium · Dec 13, 2016 at 10:56 AM 0
Share

Are you sure only ONE HitBox script and ONE collider are attached to the gameobject ?

avatar image Biggzlar Hellium · Dec 13, 2016 at 11:02 AM 0
Share

Absolutely, as mentioned above there is another collider with isTrigger=True somewhere down the hierarchy but that one has been disabled for testing.

This is also the only instance of a model using this particular script in the entire scene, and project for that matter.

avatar image Desoxi · Dec 13, 2016 at 04:00 PM 0
Share

@Biggzlar Do you have a extra collider on your character controller object? If you try deactivating it. The characterController class inherits from collider and thus, has a built in support for OnTriggerEnter() stuff. When you are using another collider too, it could be that the event is registered twice. But im not sure tho. Tell me if it helped solving your problem.

avatar image Biggzlar Desoxi · Dec 13, 2016 at 04:48 PM 0
Share

Hello @Desoxi, thanks for your response. Sadly no, as mentioned above the CharacterController is the only thing providing a collider in the entire hierarchy.

avatar image Biggzlar Biggzlar · Dec 13, 2016 at 09:12 PM 0
Share

@tanoshimi edited the question so I can't reply directly.

Actually the EnemyAgent script is just storing this public bool, not modifying it in any way.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LoBlanc · Dec 13, 2016 at 11:11 AM

I got this error sometimes and solved it by using a bool to control that it is entering the trigger only one time per collision

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Biggzlar · Dec 13, 2016 at 01:18 PM 0
Share

Hi! There is a boolean in place already, see "thisAgent.damageDealt". It has to be handled per attacking agent in order to have simultaneous hits from several sources call the triggers.

avatar image
0

Answer by arsi86 · Jan 23, 2021 at 11:00 PM

I also had multiple calls of OnTriggerEnter and OnTriggerExit... Particulary annoyiing bug. But I've noticed that it was somehow related to dynamic light I had in the scene. By dynamic light I mean a light source which was moving and changing intensity. It was light from camp fire object. Enabling this light was causing OnTriggerEnter and Exit to flicker like crazy. Anyone else had this??

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GeroNL · Jan 24, 2021 at 06:57 AM

Maybe you can do in just one of OnTriggerEnter() / OnTriggerExit(), not both. And do like this :


 class EnemyAgent 
 {
      public float GotAttackedDelay = 0.5f;//seconds
      [HideInInspector] public float GotAttacked = 0f;
      ........
      void OnUpdate()
      {
          float timelapse = Time.deltaTime;
          if(GotAttacked > 0)
              GotAttacked -= timelapse;
      }
 }



 public class HitBox : MonoBehaviour
 {
 ....
      void OnTriggerEnter(Collider other){
          if (other.gameObject.tag == "EnemyWeapon"  ) {
              EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
              if (thisAgent.GotAttacked <= 0) {
                 thisAgent.GotAttacked = GotAttackedDelay;
                  Debug.Log (thisAgent.ID + " out");
              }
          }
      }
 ....


Hope it helps.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make object react to certain triggers only 1 Answer

How Does OnTriggerEnter() Work? 0 Answers

Application.LoadLevel ("lower") loads on start-up, not when triggered; Deadline is soon HELP! 3 Answers

Deactivate/Activate FPS Movement 0 Answers

OnTriggerEnter not working properly. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges