- Home /
The question is answered, right answer was accepted
Overlapping instances of the same object trigger collision more than once
Hi Guys,
So, I've been having this problem for a while and I couldn't find anything on the topic. I have a game where you move a ball around and once in a while it picks up a popup which generates another ball. So I end up with multiple instances of the ball, which you control at the same time by tapping. So far, so good. My problem is that sometimes two balls may overlap perfectly and when they activate the trigger on the pop up two balls get instantiated.
What I need to know is how to register the OnTriggerEnter2D once in this situation, even though two balls may activate the trigger at the same time. The OnTriggerEnter method is attached to the popup object and it identifies the ball by tag:
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Ball")
{
Destroy(gameObject) ;
Instantiate(ball, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation); //instantiate ball where coin was
}
}
Answer by FortisVenaliter · Sep 29, 2017 at 04:57 PM
The easiest way would be a countdown timer set to, say, one second.
So, you only instantiate a ball when the timer is
That way, the collision trigger can effectively only fire for one ball, as after a second, the second ball should have moved on.
Hey, I used a bool ins$$anonymous$$d but it is the same principle. Thanks a lot for the suggestion:).