- Home /
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
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.
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.
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....
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
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