TNT needs to explode right? Not just visual, but physical too. I made Explosive Pinball so I should know how to do TNT explosions right? Well in 3D maybe, but not in 2D. There is no method in Unity to cause a 2D Rigidbody.AddExplosionForce.
The internet has some useful solutions though. This is a Unity forum thread stating that these people know there isn’t a method for 2D explosions, but people have solutions. Some of which I have an idea of what they do, but NO idea of how to implement.
There’s this video that would have a second circle collider as a child within the TNT and expand it per second while adding force to the relative objects.
The Unity Asset Store even had a free downloadable asset that causes explosions. Just add the already written script to any sprite that has a RigidBody2D.
The only thing with this asset is, it creates explosions where and when the mouse is clicked, and applies its forces to the objects that have this script attached.
There was a whole lot of other code in there for Android and IOS use as well, but I removed that because this is a PC game with a mouse. I tested this and it had the exact functionality that we needed for a TNT explosion, except it was caused by clicking rather than it happening with the collision of the ball. I tried to adapt this code so the TNT was detecting for the collision and colliding object was the one getting the added force. I messed with the code and tried what I thought be right. But at that point when I was working on the TNT explosion my brain had about had enough of scripting for the time being. And I wasn’t in the right mind to realize how to adapt this in a way that our game needed it to be.
I was working with a programmer on Ball Ball, he finished the task he was working on and I asked him if he could try figure this out or find a way for the TNT to work. Surely enough he got it to work not long after.
But in the process of writing this blog, where my head space isn’t so cluttered and in panic mode with the amount of things to do, I approached this problem again. I imported this asset again, changed the code down to what was needed. The AddExplosionForce() method with the 4 parameters, and an OnCollisionEnter2D method and changed one simple thing. Instead of the Vector3 objPos1 = Camera.main.ScreenToWorldPos(mousePoz). The Vector3 object would equal the colliders position.
This would work if the ball was detecting the TNT and the ball would just add force to itself. There would still need to be an explosion and adding of force to other objects like dynamic blocks and other TNT’s.
At least now when I decide to have a 2D explosion, I’ll have a much better understanding of how to make one.
Until next time –