Sticky Bomb in Unity

Sharing is caring:

Use Joints to Create a Sticky Bomb

In this Unity tutorial we’ll show you how to create a simple sticky bomb grenade. We’ll first start off by adding an empty game object directly in front of our first person controller. This will be used to instantiate our sticky bombs. Create a prefab of your sticky bomb out of a sphere that has a rigidbody attached to it.

I went ahead and changed the mass to “0.2” as we want the sticky bomb to be lighter than any other rigidbody components in our scene. Afterwards you’ll want to attach the “sticky” script to this prefab. This script will include everything we need in order to create a timed explosive grenade that will stick to other objects.

The way the script works is that once the grenade collides with another rigidbody object it automatically creates a fixed joint at that point essentially creating a “sticky bomb”. After a two second delay the bomb explodes. The scripts  and resources used in this video can be found below:

 

Javascript Files

Instantiate.js (to toss our grenades)

var grenade : Rigidbody; //our prefab
var throwPower : float = 10; //power variable determines how fast this object will be "shot out"

function Start () {

}

function Update () {

if (Input.GetButtonDown("Fire1")) {
    var clone : Rigidbody; //variable within a variable
    clone = Instantiate(grenade, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection(Vector3.forward * throwPower); //applies force to our prefab using the throwPower variable

   }
}

sticky.js (apply to sticky bomb prefab)

var radius : float = 5.0;    //provides a radius at which the explosive will effect rigidbodies
var power : float = 10.0;    //provides explosive power
var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
var explosionPrefab : Transform;
var explosionSound : AudioClip;

function Start()

{

}

function Update () {

Fire();
 }

function Fire () {

  yield WaitForSeconds(explosiveDelay);
  var grenadeOrigin : Vector3 = transform.position;
  var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //any collider within the radius of our object will feel the explosion
  var e : Transform = Instantiate(explosionPrefab,transform.position,transform.rotation);
  Destroy(e.gameObject, 2);//destroys our particle system after 2 seconds

for(var hit : Collider in colliders){  //for loop that says if we hit any colliders, then do the following below
 if (hit.rigidbody){
     hit.rigidbody.AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power
     AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
     Destroy(gameObject);//the radius and finally the explosive lift. Afterwards destroy the game object.

    }
 }

}

function OnCollisionEnter(col : Collision) {
         var stickyBomb = gameObject.AddComponent(FixedJoint);
         stickyBomb.connectedBody = col.rigidbody;
 }

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. Eric Greggs says:

    Would it be possible to get the sample project from this example? That would help a lot.

    Thank you very much in advance.

    Best Regards,

  2. Eric Greggs says:

    Hi Jon,

    Did you have time to dig into your old Unity3d project files ? Any luck in finding this one ?

    Thanks in advance,

    Eric

    • Hey, sorry a lot of my old files got deleted. I am planning on making a tutorial very similar to this using C4 explosives and I’ll provide the project files for that.

  3. Hi since unity has been updated there seems to be some errors now with the code , how would you write it now and would there be a C# version ?

    • It doesn’t have errors, Unity might prompt you to update the scripts since Unity 5 now requires you to use “GetComponent” if you’re using a rigidbody but Unity should do this for you automatically. You can also use “GetComponent(Rigidbody)” in the start function as well.

      For converting it’s relatively easy just swap some stuff out. So for variables you’d have something like “public GameObject myvariablename” and for functions instead of “function start ()” it would be “void start ()”. I may update these scripts to C# at one point but it will require some testing.

  4. Thanks so much for everything!

  5. Example on a C# please

    • Converting this to C# shouldn’t be too difficult. You’d essentially “reverse” the variables. So instead of “var float : myFloat” you’d write out “public myFloat float”. For the WaitForSeconds you can use an IEnumerator or use Invoke. The “for loop” I would typically replace with a “foreach loop” which would be nearly identical except that it says foreach. Anytime you see a variable/parameter, just write out the type first, then the name. Lastly “function” would just be “void” in this case. So “void Update” instead of “function Update”, the rest of it should be the same.

  6. Blushchicken says:

    Dear John G.
    Could I update the script to go with my npc, throwing the grenade every 5 or so seconds?
    Thank you from,
    BlushChicken Industries

    • Yes you can. If you want a sticky bomb to be instantiated every 5 seconds you can add in a check for time:

      var nextThrow : float;
      var throwRate : float;

      if(Time.time > nextThrow) {

      nextThrow = Time.time + throwRate;
      //Instantiation line
      }

Speak Your Mind

*