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