Friday, November 6, 2015

Exploding Crate Scripting

I just finished writing the scripts for the exploding crates that will appear in our game. The crates now explode when hit with Rana's fireball, set off other exploding crates that are near, and exert force on other objects in the area. I tried many different things before I was finally able to get it to work properly.

I started off, adding to the exploding crate, a child object with a disabled circle collider. My theory was, when the crate was struck but the fireball the child would activate and then trigger the same reaction in surrounding exploding crates, but for some reason the collider from the child wouldn't interact with the surrounding crates, even though the collider touched the colliders from the surrounding crates.

Next I created a prefab that was a completely seperate object with a circle collider acting as a trigger. I scripted the exploding crate to instantiate this object (which I called explosion) upon getting struck by the fireball and it worked exactly as I needed it to. Afterward I added, to the explosion object, a script that waited a very short amount of time before removing the object from the scene to reduce objects the engine needs to track at runtime.

The next big challenge was exerting explosive force on the objects around the crate. There is an AddExplosiveForce physics effect in Unity but it only works for 3D objects, colliders, and rigidbodies. Since 3D and 2D objects don't interact with each other in Unity, I tried to find a way to trigger 3D effects on 3D objects with a 2D trigger (the fireball). I did something similar to what I tried before by attaching a child, with 2D elements, that would then activate the scripts on the parent object but this failed as well.

I took to the internet and quickly found a free asset on the Unity Asset store for AddExplosiveForce2D, developed by Ananda Gupta at http://anandagupta.com. Thank you Ananda! The script was the effect I needed but the example contained within only worked on mouse click. I was able to move the key method from Ananda's script (Thanks again!) and attach it to my crate but for some reason it still wasn't working. I could hit the crate with a fireball and it would be removed from the scene but it wouldn't exert force on the objects around it.

I moved the epicenter of the explosive force and discovered that the explosive force was only interacting with the object itself and not the surrounding objects. Again, to the internet! I quickly found that you can use Physics.2D.OverlapCircleAll to create an array of the objects within a radius. By adding a layer mask I was able to make sure the explosive force would only effect the objects I intended it to. Adding in a quick while loop, I was able to iterate through the array and add the explosive force to each of the surrounding objects, using the initial object as the epicenter of force. By adding a call to this function right after the call to instantiate the explosion object, I can chain the explosive physics effect as well.

In order to simpify things I tried to create a single method that would test the objects in the radius to see if they were explosive crates before adding force to them. This way I could get rid of the explosive object altogether. Unfortunately, each time I tested this singular method, the program threw a stack overflow exception. But that's ok, because I have something that works exactly how I want and cleans up after itself. There's a couple screenshots below where you can see the explosive crates in action.

-Brandon



No comments:

Post a Comment