- Home /
Destructible GameObject OnTriggerEnter2D getting called twice
Expected Result: PowerUp GameObject floats around the screen and bounces off collision (e.g. walls) until it collides with a player, or gets destroyed by their bullets. Bullets should only do 1 point of damage per collision.
Setup: I have a PowerUp GameObject that has both a CircleCollider2D with a Bounce Material to get it to bounce off collision, and a BoxCollider2D flagged as "isTrigger" to get it to apply the PowerUp effect to the player that collides with it.
Actual Results: PowerUp GameObject works except for that it sometimes takes double damage per shot. I assume based on research that this is due to the fact I have two colliders on the same GameObject.
Request: How can I set this GameObject up to achieve the Expected Result?
Here's a code snippet that handles the damage. Basic stuff:
if (collision.CompareTag ("Laser_p1") || collision.CompareTag("Laser_p2"))
{
health--;
Destroy(collision.gameObject);
FindObjectOfType<AudioManager>().Play("powerup_hit");
StartCoroutine(Flash());
}
Answer by Walrusking16 · Mar 31, 2020 at 03:18 AM
Now I'm not an expert and am actually pretty new to unity myself, but as a fix I believe you could setup a bool like damageDelt and do something like this
if (collision.CompareTag ("Laser_p1") || collision.CompareTag("Laser_p2"))
{
if(damageDelt != true){
health--;
Destroy(collision.gameObject);
FindObjectOfType<AudioManager>().Play("powerup_hit");
StartCoroutine(Flash());
damageDelt = true;
}
}
Your answer
Follow this Question
Related Questions
trigger inconsistent behaviour? 1 Answer
Ai trigger 0 Answers
Check if a Collider2D is hit by a Trigger Collider2D 1 Answer
Health DMG 5x gameobject 0 Answers
How I can do to make the objects do not take a lot of space in the game? 3 Answers