- Home /
Prevent explosion through walls
I'm working on a top down 2d game that contains some ranged attacks that create explosions on impact.
Imagine a rocket that detonates on a timer or when it impacts a wall. When it detonates, I generate an object with a particle system and a circle collider 2d (which I scale in sync with the particle system). When the collider calls OnTriggerEnter2D, before it runs any damage/knockback/ect code, it checks if the collision is blocked. I do this using a raycast (which checks only to objects in my high and low wall layers) from the center of the explosion toward the center of the transform attached to the object passed through to OnTriggerEnter2D. If the raycast finds any collisions, I return out of the OnTriggerEnter2D function and ignore the collision.
This works quite well, except for when the rocket enters the wall so far that the center of the rocket is inside the wall when it forms the explosion. This will cause the explosion to be completely ignored on any side of an obstacle. I've tried making the RigidBody on the rocket use continuous collision detection, but it will still go too far into the wall sometimes. I've considered making the raycast ignore objects that the cast starts inside, but this will simply make it so that sometimes explosions will go through walls, which just changes the problem.
I've also considered keeping the last position of the rocket each FixedUpdate and checking if the current position is inside a wall on detonation, if so, use the previous position.. but this feels very cludgy and can potentially create weird scenarios where the explosion looks too far from the rocket.
Does anyone have any advice?
Your answer
![](https://koobas.hobune.stream/wayback/20220612074124im_/https://answers.unity.com/themes/thub/images/avi.jpg)