A quick video I put together to show my workaround for the missing AddExplosionForce2D in the Unity engine. It’s probably of interest to nobody out there but it gives a good idea of how Unity works and the very basics of making things move around on the screen.
Hi there,
ReplyDeletejust wanted to say thanks alot for this video. It's exactly what i was looking for. I didnt get it to work in my project, but it i might be on the right track. :)
Hope that the unity-team includes addexplosionforce2d soon.
Thanks again, and keep up the good work!
Thanks Tobias. Nice to know someobody found this useful. To be honest, since I did this, I'm been messing with it more and I don't think I'll actually want explosionforce2d now! This method is so customisable.
ReplyDeleteAnd if you need help figuring why it's not working, I'm always here. ;o)
David
Unless I got something wrong there is a problem with this in that you use directional vector with it's unedited size (the further away from the explosion, the longer the vector), resulting in inverted knockback behavior - objects closer to the bomb don't get pushed as much as those further away.
ReplyDeleteI can't quite figure out how to edit the values to invert them as to make the explosion have stronger knockback closer to the bomb and cool down with time. Just using inverted value results in strange behavior, where there is very strong knockback close to the center, albeit cooling down very very quickly.
Hmmm... Thanks Drury for spotting this. I think you're right. This might be working despite not having that falloff. It's a while since I did this so please forgive me if I get this wrong but I know it worked because I used it in my last game and objects did move slowly the further away from the point of detonation. I think the inverted behaviour (actually makes quite a good sucking mechanism, which I also used) is simply a result of getting the subtraction in the wrong order: bomb - target rather than target - bomb. I never remember which way it's supposed to go. ;)
ReplyDeleteRegarding a falloff in force, I think it's probably easy to implement. I'd take the direction vector produced when you subtract the two points (the bomb and the target). Obviously, as you point out, this gets bigger the further way you get from the source but you could get that distance using
float distance = Mathf.Abs(Vector2.distance(bomb, target));
Now we have the distance and the direction vector but, as you say, it's the wrong way around but it should be easy to invert it.
float maximum_bomb_radius = 20f; // the point where the explosive force becomes zero
float power = maximum_bomb_radius - distance;
if (power < 0)
power = 0;
Vector2 explosive_force = direction * power; // Here you could also add in any constant to determine the strength of your explosion.
This is off the top of my head but if you want me to code this, I'll sit down tomorrow and do that.
Incidentally, there's also an additional parameter to the AddForce method, which I didn't know about at the time but might be helful. If you use AddForce(direction, ForceMode2D.Impuse) you get a force which acts like a sudden brief impulse, as you'd expect from an explosion.
Hope that helps but feel free to email me if you want me to code it and prove it works.
Thanks! It works as intended now.
ReplyDeleteYou only forgot to use direction.normalized in explosive_force calculation. If you use raw direction, it will take into consideration it's size, and end up somehow nullifying itself with the value of power, causing explosive_force to remain constant at all times (eludes me how exactly, but tests suggest it indeed does).
So thanks again for the help, this is very useful.
I just want to say thanks you for sharing this video with us.
ReplyDeleteCan u plz post the code of ur awsm video the explosion force workaround plz the improved one
ReplyDeletethnx
Really sorry. I've checked my work folder and that demo I wrote isn't there. I had a hard drive die about twelve months ago so I imagine it was among the many things I lost. There's not that much code and all of it is in the video, if you really need it. Unfortunately, I have work piling up otherwise I'd type it out again for you. :)
ReplyDeleteOh what happnd with mine was that the bomb just dissapears without doing anything even though i wrote all the target-bomb stuff
ReplyDeleteEmail me or post your script and I'll take a look in the morning. Sure I can fix it quickly.
ReplyDeleteEr did my that source code reach u
ReplyDeleteNot as far as I can see. Send it to thespineblog@gmail.com and I'll keep an eye out for it. I'll have a look as soon as it arrives.
ReplyDeleteIncidentally, did you check that you'd correctly selected 'trigger' for the colliders that needed it?
Only for circle collidr of the bomb rit
ReplyDeleteOkay. Let me just see if I can quickly recreate the script... I might be able to do it quickly and send you a working version that way.
ReplyDeleteI have sent the mail to ur above specified account
ReplyDeletethnx in advance
Hopefully u got my mail
ReplyDeleteYep. Looking at it now but I have to remember a lot of things I've forgotten about Unity. Not used it in a few months...
ReplyDeleteCool by the way i think i found one mistake ie the addforce function isnt it supposed to have x and y component
ReplyDeleteI might hv skipped it i guess but im not able to undrstnd that
ReplyDeleteThe vector you created by target-bomb has a direction. It's correct. I think the problem has something to do with your timings. Not quite figured it out but I think it's exploding too quickly before it affects the other bodies.
ReplyDeleteI think ur rit if i give delay as 1 it dissapears damn fast so i tried givng it Higher value but the green collider thing is explodng smthng lik that not affectng other.bodies
ReplyDeleteI've just sent you an email. I think your script is okay but your timings are probably off. Start small and work up. Start with a 0 sized radius so the explosion starts at a point. Tinker with the value of explosion rate, so it stars small and builds up. You could also change the settings of Time in your project but you probably don't need to do that. FixedUpdate doesn't happen once a frame but *many* times a frame. There's a chance that the explosion could be happening in the space of a single frame. However, I don't think that's the case.
ReplyDeleteI quickly created a scene here with a bomb and a few boxes and your script worked once I started the radius at 0.
Also, make sure that you have 2 colliders on your bomb. One should be a box collider which stops it falling through the floor. The other is the circle collider which has to be a trigger. This is the one we increase so it it passes through objects but triggering their movement.
hey david
ReplyDeleteim still getting trouble in that code
well its that the the explosion is not taking place slowly in mine its lik
its skipping frames and only final position is being shown.but in ur video it could be made slow. i tried to adjust all the values but still the problem persists.
I'm really pressed for time today but if you send me the project, I can have a look. It's the only way I'll really be able to figure it out. It could be something really simple like you've played around with your settings for Fixed Update or even some bug with my code. If you send it me, I'll probably get it working.
ReplyDeletefinally figured it out
ReplyDeletei think the error occured since i was using spheres instead of cubes as other objects. sphere also has circle collider in it. when i removed that the explosion is working quite fine.
but is there any way i could use circle collider for other objects without getting error?
I honestly can't remember (I've not been using Unity for the past few months) but I would think it would be possible to move the colliders to a different element in your object hierarchy, allowing you to have multiple circle colliders. Failing that, you could try polygon colliders, I think. Not sure what the new Unity has added/improved in that respect.
ReplyDelete