Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rombout · Nov 21, 2012 at 04:42 PM · c#triggerontriggerexit

OnTriggerExit not triggered when object/trigger inside is destroyed c#

I am trying to do the following: I've got an enemy with a sensor that stops walking when it enters a trigger of a destructible wall. It then starts to attack. When the wall's health is 0 I want the enemy to start walking again. So far so good...on paper.

OnTriggerEnter makes the enemy stop walking, OnTriggerStay makes the enemy attack, OnTriggerExit should make the enemy start walking again.

The issue I am getting though is that the OnTriggerExit of enemy sensor is never triggered. When I "get rid" of the wall (by setting it to a layer that has no collision with the enemy or destroy it) the enemy stills says it is stuck in the wall trigger according to the debug.log. When I animate the wall to move the enemy will start moving again until it hits the wall's new position. Moving it a gazillion units away from its current position, as suggested in other threads, doesn't really seem like a nice sollution, right?

I am getting the feeling as if getting rid of the wall, while the enemy's sensor is static, doesn't properly trigger the OnTriggerExit as I'd expect.

Another similar issue occurs where I have a sentrygun shoot when an enemy enters its trigger. Everything works perfectly but as the enemy is destroyed in the trigger, the sentrygun never gets the OnTriggerExit which results in a sentrygun that keeps on firing as if there is no tomorrow.

Any ideas what I am missing here? I am using C# by the way. This is the script that is attached to the enemy sensor.

 void OnTriggerEnter (Collider other)
     {
         walk = false;
     }
     
     
 void OnTriggerStay (Collider other)
     {    
         if (other.gameObject.CompareTag("LevelDestructible"))
         {
             other.gameObject.SendMessage("ChangeHealth", damage * Time.deltaTime, SendMessageOptions.DontRequireReceiver);
             Debug.Log("collide with destructible");
         }
         
     }
     
     void OnTriggerExit (Collider other)
     {
             walk = true;
     }
Comment
Add comment · Show 1
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 dannyskim · Nov 21, 2012 at 05:34 PM 0
Share

$$anonymous$$oving the object's transform.position isn't really an elegant solution when you think about it, but the way the physics steps works inside of Unity for your specific scenario does make sense.

If you're relying on OnTriggerExit, it's honestly the best approach. As long as your call ordering is correct, it should fire reliably if done correctly. I've done it before, and it plain works.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ThePunisher · Nov 21, 2012 at 05:59 PM

Why don't you simply get a target with OnTriggerEnter of whatever type it is you have (like Wall) while the wall's health is still greater than 0 or while you still have a target, continue to attack the target in an update loop. If you lose your target or your target's health reaches a value, then stop attacking and continue movement.

I think you are misusing the OnTriggerStay method with what you are doing. A better example for how to use it would be the following.

You have a particular room filled with a substance that deals damage to your character. If your character enters (OnTriggerEnter) the collider associated with the substance, the player takes an initial amount of damange. Now, you can use OnTriggerStay to make the player take periodic damage while he remains in the substance/collider.

Comment
Add comment · Show 2 · 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 Rombout · Nov 22, 2012 at 08:43 AM 0
Share

Thanks for the reply! I could use the target method as you described but then I'd have to check every frame if the target is still within a certain distance or area. $$anonymous$$ore important for other moving objects that walk away from the enemy than a static wall, but still. Isn't this what the triggers would be ideal for to use?!

Your last example doesn't seem a whole lot different from the current implementation, or am I missing something? If the player in the room dies, the substance dealing the damage would still think the player is in the room because it will, at least in my case, not receive a OnTriggerExit if I destroy the player object.

Thanks again for your thoughts on the issue, I'd love to "solve" it and get on with the project :)

avatar image ThePunisher · Nov 26, 2012 at 06:26 PM 0
Share

Well you wouldn't specifically target a Wall type, the object instance would be a Wall but it would be referenced as a base class/interface object like (TargetableObject) on which you can do this like Buff, Damage, Destroy, $$anonymous$$ove. This way anything your enemy targets can be handled by the code.

As for the other comment, the player's object would be destroyed or moved so it wouldn't trigger that method anymore, and you wouldn't need to depend on your OnTriggerExit.

You can't exit the collider because you aren't walking and you don't start walking until you exit the collider, see where the logic breaks?

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

11 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 avatar image

Related Questions

Distribute terrain in zones 3 Answers

Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management) 2 Answers

Multiple Cars not working 1 Answer

OnTriggerEnter Not Working 1 Answer

How to get trigger input on an Xbox controller (Mac) 0 Answers


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