- Home /
The question is answered, right answer was accepted
Instantiated Bomb Not Exploding
I have already figured out how to launch a bomb, but I'm having trouble with running code on the instantiated object. I made it so that the bomb would
Wait
Add an explosive force
Destroy itself
But it only waits, and then disappears.
Here's some code:
void Update()
{
if (time >= timeLimit)
{
RB.AddExplosionForce(explosiveForce, bomb.transform.position, explosionRadius);
Destroy(bomb);
}
time += Time.deltaTime;
}
Does the explosion only get applied to the rigidbody or something? Send help
Answer by GuardianWorld · Feb 20, 2019 at 11:57 PM
if you remove the Destroy(gameobject) you placed, the rigidbody that is receiving the explosive force is the bomb. you will see that the object (bomb) was floating after "Exploding".
The AddExplosionForce applies a force that simulates explosion effects to the RigidBody, not to the objects around it.
For that to work, you could:
A) you could create a collider (trigger) around the bomb, and after the time hits it's limit, it will give a explosion force to every rigidbody inside it.
B) You could use a overlap sphere and get all colliders inside and them apply force to them. Code i did way ago:
private Collider[] hitColliders;
public float blastRadius;
public float explosionPower;
public LayerMask explosionLayers;
void OnCollisionEnter(Collision col)
{
//Debug.Log(col.contacts[0].point.ToString());
ExplosionWork(col.contacts[0].point);
Destroy(gameObject);
}
void ExplosionWork(Vector3 explosionPoint)
{
hitColliders = Physics.OverlapSphere(explosionPoint, blastRadius, explosionLayers);
foreach (Collider hitCol in hitColliders)
{
//Debug.Log(hitCol.gameObject.name);
if (hitCol.GetComponent<Rigidbody>() != null)
{
hitCol.GetComponent<Rigidbody>().AddExplosionForce(explosionPower, explosionPoint, blastRadius, 1, ForceMode.Impulse);
}
}
}
This code have its flaws, as i did it way before. You could remove the ForceMode.Impulse, or change it (see documentation for this one, it's kinda useful all the modes it have.). This mode i used is a bit more hard to understand, but explanations:
A) It get all the colliders that it hit.
B) Explosion Radius and power;
C) Only hit things in a determined layer (Player, terrain, objects etc.)
It's useful, but first: Your player do have a rigidbody, if the player's rigidbody is Kinematic, then you need to disable it to apply force and after the player hits the ground, you need to enable it again or else it will bug.
If the player is not kinematic (meaning it will only use physics when code dictates to), then the player need to have its X and Z rotation fixed, otherwise he will start spinning like crazy.
Edit: With this code, the bomb will explode after contact with the ground (OnCollisionEnter), you can change that to an update() where it will after some time, explode like normal.
Follow this Question
Related Questions
Earth quake with physics? 1 Answer
Rotation problem with spawning bullet 2 Answers
OnCollisionEnter is slow... 2 Answers
I need help with rotation 2 Answers
Rigidbody load problem 1 Answer