- Home /
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));
bombexplosion.Play();
rigidbody.AddExplosionForce(10000.0f,position,5000.0f);
Destroy(this.gameObject);
}
}
}
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?
Answer by Screenhog · Jan 27, 2014 at 08:42 PM
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.
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.
Answer by Kiloblargh · Jan 27, 2014 at 08:43 PM
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 Vector3.zero
, 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.
So i did some reading, and I used tags, and worked out the problem. The only thing is now, all the explosions use the same particle system, ins$$anonymous$$d of the 2 different ones I have. Here is my script:
public void OnTriggerEnter(Collider collider) {
EnemyCube = GameObject.FindGameObjectWithTag("Enemy");
Vector3 enemy_position = collider.transform.position;
Destroy(collider.gameObject);
Vector3 explosionForce = transform.position;
Collider[] colliding = Physics.OverlapSphere(explosionForce, 500.0f);
foreach (Collider hit in colliding) {
if (hit && hit.collider.gameObject.CompareTag("Enemy")) {
hit.rigidbody.AddExplosionForce(5000.0f, explosionForce, 500.0f);
ParticleSystem explosion = (ParticleSystem)Instantiate(EnemyExplosion,enemy_position,Quaternion.Euler(100,0,0));
explosion.Play(withChildren:false);
}
if (hit && hit.collider.gameObject.CompareTag("Bomb")){
hit.rigidbody.AddExplosionForce(10000.0f, explosionForce, 1000.0f);
ParticleSystem bombexplosion = (ParticleSystem)Instantiate(BombBlast,enemy_position,Quaternion.Euler(100,0,0));
bombexplosion.Play(withChildren:false);
}
}
}
How can i use the 2 different particle systems?
Answer by fafase · Jan 27, 2014 at 08:45 PM
See the docs: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddExplosionForce.html
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.
Your answer
Follow this Question
Related Questions
Problem with AddExplosionForce / Exploding 1 Answer
chain of explosions - Destroying gameObjects in range 2 Answers
How can I destroy a object(like house) with a bomb? 0 Answers
AddExplosionForce won't have any effect. 1 Answer
bomb effect 2 Answers