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 LeftyTwoGuns · Nov 13, 2015 at 04:53 AM · physics2dcollider2ddamageontriggerenter2d

How do you get OnTriggerEnter2D to activate only once?

I have an enemy script with OnTriggerEnter2D, very simple like so:

 void OnTriggerEnter2D(Collider2D col)
     {
         if (col.CompareTag("Player"))
         {
             //do damage stuff
             DoKockback();
         }

DoKnockback is a function in another script that knocks the player back on taking damage. This happens instantly but my problem is even in the mere second it takes to knock the player back, OnTriggerEnter can trigger 2-3 times, applying damage to the player 2-3 times. I'd like the damage to only be applied once before getting knocked back. Any suggestions on how I can do this?

Comment
Add comment · Show 2
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 Gohan1 · Nov 13, 2015 at 06:17 AM 0
Share

are you sure you dont have duplicates? this hapned to me when I have two colliders on one game object

avatar image dr_adrii · Nov 13, 2015 at 08:44 AM 0
Share

How are you implementing the knock back? I'm using a material and applying it to the prefab and it works fine and players get damaged only once.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LeftyTwoGuns · Nov 14, 2015 at 09:43 PM

I've appeared to have fixed my issue by switching around the isTrigger on the colliders. Before the enemy collider was set to isTrigger and the player collider wasn't. However, setting the player collider to isTrigger and disabling enemy isTrigger results in only one OnTriggerEnter hit. Thanks for your suggestions, guys.

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 rajavamsidhar_gvs · Nov 13, 2015 at 09:27 AM

if i understand your question comparetag is like

CompareTag Is this game object tagged with tag ?

so player own objects also may hit the collisions. so you have to avoid that tag issue. use this

 void OnTriggerEnter2D(Collider2d col)
 {
      if(col.gameObject.tag("Player"))
     {
         //do ur stuff
         DoKnockBack();
      }
 }

and OnTriggerEnter2D occurs only when hits. check rigidbody settings and tag settings too.

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 Guppie1337 · Nov 15, 2015 at 05:27 AM

I've had the situation where it would call multiple times per collision as well. A solution would be to use a bool to prevent it from firing more than needed.

  void OnTriggerEnter2D(Collider2D col)
  {
     if(col.gameObject.tag("Player"))
     {
         if (isColliding)
         {   //Prevention happens here. (Only for 1 frame (See Update()))
             return;
         }
 
         DoKnockBack();
         //Bool causing the prevention.
         isColliding = true;
      }
  }
 
 void Update ()
 {
     //Set isColliding to false on the next frame.
     isColliding = false;
 }

What this does is basically allows the interaction to happen for the frame it ACTUALLY happens on. Sometimes you will get multiple calls per frame, and that is what we need to prevent. Allowing the Update method to set isColliding to false ensures you will be able to call a collision again. Hope this isn't too confusing.

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

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

Related Questions

Can you check again something that is already in the IsTrigger collider ? 2 Answers

Physics2D bug or something I can avoid? 3 Answers

Can CompositeCollider2D not be disabled? 1 Answer

2DColliders don't display properly in scene view 1 Answer

OnTriggerEnter2D does not work on inactive objects? 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