Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 JRtheUnity · Feb 20, 2019 at 03:46 PM · rigidbody2dontriggerenter2distrigger

onTriggerEnter only fires once if player doesn't move

So I'm building an 2D metroidvania-like action game.


I have a player, with:


  • rigidbody2d

  • a polygon collider, non-trigger

  • a box collider, is trigger, enabled = false, this one is the attack collider


And when I hit attack key, I set enabled to true for like 0.5 seconds, and then set it back to false.

I kept an eye on the check box besides the collider, and I'm sure this set enabled true and false is working properly.


Then I create another target object, with:


  • a polygon collider, non-trigger


When the target object has onTriggerEnter2D fires, I log it.


So ideally I will make some damage on the target when the onTriggerEnter2D fires.


However, if I let the player stand still and hit the target multiple times, onTriggerEnter2D will fire only once, I have to move my player a little bit, and hit target, to fire the function again.


I also logged it when onTriggerExit2D fires, so I am sure it exits. it just doesn't enter anymore.


Another thing is, if I attack fast and hit the target continuously, the function does fire as many times as I hit it.

but if i stop a while (like 1 second?), and I hit it again, function doesn't fire. i still have to make the player walk a little bit to fire the function again.


this situation happens only when:

( all collider on the target object are non-trigger ) OR ( target has rigidbody2D attached )

If I remove rigidbody2D, and add one isTrigger collider on the target, this thing will not happen.


Anyone can help me with it? I have no idea what causes this.

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Feb 20, 2019 at 04:02 PM

Good day.

I think you are confused with OnTriggerEnter and OnTriggerStay. AS its name says, OnTriggerEnter, is executed when enters, so only once. OnTriggerStay, is executed every frame that the collider is inside.

GoodBye!

Comment
Add comment · Show 4 · 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 JRtheUnity · Feb 20, 2019 at 05:39 PM 0
Share

Hello! so for each attack, I enabled the trigger collider for 0.5 second and then disabled it. therefore for multiple attacks, it should fire onTriggerEnter multiple times, right?

avatar image JRtheUnity · Feb 20, 2019 at 05:43 PM 0
Share

And i'm counting on the onTriggerEnter to calculate the damage, therefore I'm not using onTriggerStay here.

avatar image dcw2001 JRtheUnity · Feb 20, 2019 at 06:28 PM 0
Share
    public class DamageDealer : $$anonymous$$onoBehaviour
     {
         float timer = 0f;
         float timeBetweenAttacks = .5f;
     
     
         private void Update()
         {
             timer += Time.deltaTime;
         }
         void OnTriggerStay(Collider other)
     {
             if (timer >= timeBetweenAttacks)
             {
                 Attack()
             }
     }
         void Attack()
         {
                 timer = 0f;
         }
     }




Hello JRtheUnity,

If you would like to limit the amount of times you can attack when an object is inside a trigger, you could use a timer! This is quite similar to how the player shoots from the Survival Shooter tutorial.

To explain myself, what I am doing is setting up a timer which equals zero and continues to increase as the game runs. If another collider is found present in my Trigger, the OnTriggerStay function will be called, which calls once every frame. But that would not be good because then the object would take damage once a frame. So, the next portion of my script says, if the timer (which is updated once a frame to have the time it took to go from the last frame to the current frame added to whatever timer currently is) is greater than or equal to the timeBetweenAttacks variable (in this case, .5), call the Attack() Function. Inside the Attack Function, write some code. I would recommend writing if (Input.GetButtonDown("ButtonName")) and then however method you decide to deal damage. At the end of your Attack Function (inside of your if Input statement), make sure to set the timer back to zero, so it now has to wait another .5 seconds before the OnTriggerStay function can call attack again. Let me know if this helps!

avatar image JRtheUnity dcw2001 · Feb 20, 2019 at 10:13 PM 0
Share

Hello dcw2001, Thank you for sharing the code and the thoughts! however, i think it's my bad that I didn't make my question clear. I will update the question later.

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

101 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 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 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

Unexpected behaviour of OnTriggerStay2D 0 Answers

Rigidbody2D isKinematic effect by growing mass to huge number ? 0 Answers

OnTriggerEnter2D fires multiple times when Animator component is on 3 Answers

OnTriggerEnter2D misbehaves when called quickly 0 Answers

Why is my 2D Collision Delaying or Missing? 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