Delay of destroy(gameObject) on collision

Hi there, I’m doing the health/damage part og this Glitch Garden and there seems to be a delay on my collision or Destroy(gameObject) code. When playing the game, the zucchini does destory the Lizard, but it flies a little further to the back. My code is exactly as Rick wrote it… Any idea why?

Best Nicklas

Hi Nicklas,

Without any further details, my first two thoughts would be the position of colliders on your GameObjects and the collision detection mode being used.

The former is really easy to see. From the toolbar, run your game and have an attacker and defender in the same lane, wait for the defender to fire a zucchini, when its close (but not too close) to the attacker, pause the game. Switch to Scene view and then use the step button to step through frame at a time. You will be able to see the colliders on each sprite if you select them, so, select both in the Hierarchy and you’ll be able to see whether they make contact where you would expect, or whether perhaps one of the colliders isn’t where it should be which displays the sprite travelling further than it should.

Happy to take a look if this isn’t the issue.

Hi Rob, yeah I did check each collider, and they do collide, but both objects are destroyed a little after the collision :thinking:

I’ve zipped the entire project if you don’t mind having a look :pray:

Best N

1 Like

Hi Nicklas,

Thankfully this is a really straight forward one to resolve.

Here’s what I did…

I ran your game and waited for an attacker to be spawned in the lane with the defender in it, as soon as one was there a projectile was launched and moved towards the attacker. As it got close I paused the game and then stepped through frame by frame as I suggested above.

The two BoxCollider2Ds made contact but I noted that at the point when they did the attackers health was not reduced, the attacker has a health of 80 and the damage dealt by your project is 100, so potentially I should have seen the attacked destroyed. I initially considered that perhaps the collision detection mode could be responsible here, so I continued to step through. 14 frames later and the projectile’s BoxCollider2D had left the attackers BoxCollider2D, it was only at that point that both the attacker and the projectile were destroyed.

This was really the big clue. To summarise, two colliders make contact, but only when they are no longer making contact does something happen. I guessed that perhaps you were not using the OnCollisionEnter2D method, and indeed you are not, you are actually using OnCollisionExit2D. You code is therefore doing exactly what you have asked it to do, note the collision, do something when that collision isn’t happening anymore.

private void OnTriggerExit2D(Collider2D otherCollider)
{
	var health = otherCollider.GetComponent<Health>();
	var attacker = otherCollider.GetComponent<Attacker>();

	if (attacker && health)
	{ 
	    health.DealDamage(damage);
	    Destroy(gameObject);
	}
}

The solution to this would be to change OnCollisionExit2D to OnCollisionEnter2D. I’ve not yet looked at the remastered version of Glitch Garden, so I’m not sure if this was just a typo on your part or whether there is perhaps a reason @Rick_Davidson has asked you to use this method? It could be, for example, to give the appearance of the project not just vanishing the moment there is a collision, which may look a bit artificial. However, you could get around that issue by offsetting the BoxCollider2D slightly from the sprite, e.g. the sprite is slightly ahead of the BoxCollider2D, so the sprite will overlap the attacker, perhaps somewhere between left edge of the attacker’s sprite and it’s mid point.

image

With the above, be aware that it may be possible for the projectile to collider with the defender and cause damage to it, although this didn’t happen in my example below. You should either adjust the BoxCollider2D accordingly, consider the position of the Gun GameObject, or, more ideally, use Layers and update the collision matrix under Project Settings / Physics 2D to prevent friendly projectiles hurting defenders.

In the original version of this project I think they just made contact and disappeared straight away.

In any case, straight forward to resolve.

private void OnTriggerEnter2D(Collider2D otherCollider)
{
	var health = otherCollider.GetComponent<Health>();
	var attacker = otherCollider.GetComponent<Attacker>();

	if (attacker && health)
	{ 
	    health.DealDamage(damage);
	    Destroy(gameObject);
	}
}

Here’s a video of the frame by frame collision detection;

Here’s a video after changing the OnCollisionExit2D method to OnCollisionEnter2D;

Here’s a video where the method has been changed and the projectile prefab has had the BoxCollider2D’s offset adjusted on the X;

Hope this helps :slight_smile:

Privacy & Terms