Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a bomb

Discussion in 'Scripting' started by Alienchild, Apr 4, 2011.

  1. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    I am contemplating ways to create a bomb-like object in Unity that sends out a shockwave. Objects close to the blast will be pushed with X amount of force, but the force falls of with a square of the distance (or a linear curve). Not quite sure how to start on this, other then perhaps have a sphere collider on the actual bomb object and perhaps test if anything is within its radius. Anyone got a tip or two?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Rigidbody.AddExplosionForce has that built in: "Applies a force to the rigidbody that simulates explosion effects. The explosion force will fall off linearly with distance to the rigidbody."

    See the example snippet in the docs and it should do exactly what you're talking about. (It uses Physics.Overlapsphere first, which is probably a better solution that using an actual sphere collider).
     
  3. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    Rigidbody.AddExplosionForce

    The force falls off linearly, so you might not be able to use it, but if you want a quick solution that might help. Adding the force isn't too hard, and you could use an extension method to make it flow naturally in C# or with just a little more work in js.

    Code (csharp):
    1.  
    2. //This is just something that I'm putting down.  This should behave similar to what you want, but it probably isn't perfect.
    3.  
    4. public static class ExtensionMethods {
    5.      public static void AddExpExplosionForce (this Rigidbody rB, Vector3 position, float force, float upwardEffect, float falloff) {
    6.      //position - where the blast occurs
    7.      //force - how much force would be applied if the object were ontop of the explosion
    8.      //upwardEffect - pushes the object up for a cool effect.
    9.      //falloff -  a higher number increases the falloff due to the negative exponent in the equation.
    10.             Vector3 posFromBlast = rB.position - position;
    11.  
    12.             float distFromBlast = posFromBlast.magnitude;
    13.             float actualForce = force * Mathf.Pow(falloff, -distFromBlast);
    14.  
    15.             Vector3 upwardForce = Vector3.up * upwardEffect;
    16.  
    17.             rB.AddForce(posFromBlast.normalized * actualForce + upwardForce);
    18.      }
    19. }
    20.  
    example call (you must use C# to call extension methods, but remove the this keyword and you should be able to call it from js by passing the rigidbody as a parameter.

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour {
    3.       function Update () {
    4.               if(Input.GetButtonDown("Fire1") ) {
    5.                      rigidbody.AddExpExplosionForce( Vector3.zero , 250 , 100 , 1.2 );
    6.               }
    7.       }
    8. }
    9.  
    or javascript, remove the "this" modifier in the method declaration:

    Code (csharp):
    1.  
    2. function Update {
    3.       if(Input.GetButtonDown("Fire1") ) {
    4.              ExtensionMethods.AddExpExplosionForce(rigidbody, Vector3.zero, 10 , 5 , 1.2 );
    5.       }
    6. }
    7.  
    EDIT: legend posted while I was writing this. But this code should fall off exponentially as opposed to linearly. I have no idea how physically accurate it is. It's a proof of concept more than anything else.
     
    Last edited: Apr 4, 2011
  4. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    Thank you legend411 and Peter G. I ended up with implementing AddExplosionForce though modifying the script a little to better work in my game environment. The result is very satisfying, though I wish I had better control over the falloff. I will be experimenting with your code Peter, and create an alternative script so I can test both and see which I like best.

    Again, thank you for spending your time writing the reply, it is highly appreciated :)
     
  5. simplicitydown

    simplicitydown

    Joined:
    Dec 11, 2015
    Posts:
    6
    The posted code does not work. In C#, remove WHAT keyword to make it work? Sorry but that seems super vague...
     
    zeropointblack likes this.