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 /
This question was closed Nov 19, 2016 at 09:09 PM by SgtShadowWalker.
avatar image
0
Question by SgtShadowWalker · Jun 24, 2016 at 05:19 PM · c#collisiontriggerdelaycoin

OnTriggerEnter2D/OnCollisionEnter2D - delay

Hi,

This is a problem that started a while ago and I never got to figure out what caused it. I have a coin prefab in my game with 2 box colliders (2D); 1 solid (with a bouncy material) and 1 trigger collider.

I have tried both OnTriggerEnter2D and OnCollisionEnter2D and both seem to have some sort of delay. With OnTriggerEnter2D, I can bounce the coins on my players head, push them around, etc. and they won't disappear after being in contact with the player for a couple of frames. OnCollisionEnter2D has a similar problem, except that the player gets blocked for a frame or 2 when it hits the coin. In both instances, the player picks up the coin, but the delay is annoying and messing with the fast-paced flow of my game.

This is the code I have for it:

     void OnTriggerEnter2D (Collider2D PlayerHit){
         if (PlayerHit.gameObject.CompareTag ("Player")) {
             playerAudio.PlayOneShot (CoinPickup);
             PlayerManager.giveCoins(1);
             Destroy (gameObject);
         }
     }
 
     void OnCollisionEnter2D (Collision2D PlayerHit){
         playerAudio.PlayOneShot (CoinDrop);
         if (PlayerHit.gameObject.CompareTag ("Player")) {
 
             playerAudio.PlayOneShot (CoinPickup);
             PlayerManager.giveCoins(1);
             Destroy (gameObject);
         }
     }

There is also destroy-timer active from the moment the coin prefab gets instantiated.

Is this a problem with Unity? Because when I made this script 2 months ago, the issue was not there.

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

  • Sort: 
avatar image
0

Answer by tanoshimi · Jun 24, 2016 at 08:10 PM

My guess is that it's your playerAudio.PlayOneShot (CoinPickup); that's causing the delay. PlayOneShot() instantiates a new object, which is slow anyway, and then depending on the audio format of your audio clip, you then might be waiting while that clip is loaded and decompressed in memory. You could easily verify if this was the case by using the profiler, or just commenting out those lines and seeing if it makes a difference.

Comment
Add comment · Show 3 · 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 SgtShadowWalker · Jun 24, 2016 at 08:17 PM 0
Share

Thank you for your comment, but that's not the cause, sadly. I'm going to quote myself from another comment I replied to 10 $$anonymous$$utes ago;

Disabling everything but Destroy(gameObject);, enlarging the trigger collider, changing the way it detects tags and so on. Nothing works and the delay is still there. I'm really starting to think that it's Unity's fault as the same script worked perfectly fine 2 months ago.

I already tried disabling everything and it's not as if there's a short lag spike in the game caused by something being instantiated. It's running smoothly with a $$anonymous$$imal of 100fps in the editor.

Another thing is that I didn't have this problem 2 months ago with the exact same script, so I'm starting to suspect that Unity is the cause.

avatar image tanoshimi SgtShadowWalker · Jun 24, 2016 at 08:23 PM 1
Share

Code doesn't just change behaviour because it gets old over time. There must be something that you've changed in the last two months....

avatar image SgtShadowWalker tanoshimi · Jun 24, 2016 at 08:31 PM 0
Share

I understand and accept that there's a possibility of me having done something to it, but when taking the coin prefab and coin script from my 2-month old backup from when it still worked, the problem persists.

Again, it might be something that I have done, but I am certain that I didn't change anything about the script or prefab after I made it since there was no reason for me to do that.

I've read about multiple people complain about a delay with OnTriggerEnter2D on Unity Answers, so it might be a bug with Unity. But for now I'll just assume it's a problem I created as that means I can fix it one way or another.

Follow this Question

Answers Answers and Comments

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

Delay Jumping 3 Answers

My spikes don't damage the player 2 Answers

Distribute terrain in zones 3 Answers

Character Fails To Jump Sometimes 1 Answer

Collision triggers do not work on child gameobject 3 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