How to create a time bomb in Unity?

I am making a small arcade game, and I am trying to randomly instantiate a bomb that will explode after a while. But, when it explodes, it doesn’t add the explosion force. Here is the code:

using UnityEngine;
using System.Collections;

public class Bomb : MonoBehaviour {

	public ParticleSystem BombBlast;
	private float timer;

	// Use this for initialization
	void Start () {
		timer = Random.Range(5,10);
	// Update is called once per frame
	void Update () {

		if(timer > 0) 
			timer -= Time.deltaTime;
		else {
		Vector3 position = this.transform.position;

		ParticleSystem bombexplosion = (ParticleSystem)Instantiate(BombBlast,position,Quaternion.Euler(100,0,0));

No explosion force is being added. Also, I want to make the bomb solid in the sense that objects should not be able to pass through it. All objects except my player are stopped by it, but my player just goes straight through. How can I stop that?

  1. It looks to me as though you are applying a force to a GameObject’s rigidbody, and then immediately destroying that GameObject. You need to apply the explosion force to the actual rigidbodies that will show the explosion.

  2. I’m guessing that the player’s movements are being handled by directly changing the transform.position of the player? If so, that will ignore physics collisions. You need to be moving the player with a CharacterController or some other way that will pay attention to physics.

Because rigidbody refers to the bomb’s own rigid body, it won’t blow up anything else except the bomb.

And it probably won’t even do that, since you are applying the force to the bomb’s exact center, so the direction it will be pushed is, IOW nowhere.

It’s clear that you do not understand most if any of the script you posted- you are going to have to read the manual, in its entirety, but start with the physics section- if you don’t get the syntax, you will also need to read a C# reference. I know it’s boring, you want to just jump into getting the game made and blowing things up, but you will never get to that point until you do the boring technical reading part first.

See the docs: Unity - Scripting API: Rigidbody.AddExplosionForce

In order to apply the force you need to fetch all the rigidbodies and apply the force.

AddExplosionForce is just a shorthand to avoid all calculations of distance from the center of explosion and attenuate using log or linear and so on. The method does it all for you but you still need to pass the explosion value to the rigidbody.