- Home /
AddExplosionForce won't have any effect.
Hey, I'm making a Sidescroller-Shooter-Game.
So I have my Character equipped with an RPG, that is shooting missiles. If the Missile hits a collider, it will instantiate an Explosion and Destroy itself. The Explosion got a Script attached, that will call AddExplosionForce on void Start.
I actually took the Script from the Scripting reference:
using UnityEngine;
using System.Collections;
public class ApplyExplForce : MonoBehaviour {
public float radius = 5.0F;
public float power = 10.0F;
public void Start() {
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders) {
if (!hit)
if (hit.rigidbody){
hit.rigidbody.AddExplosionForce(1000, explosionPos, 10, 3.0F);
}
}
}
}
In the next step I created a Cube and added a rigidbody to it. Nothing more. But if i shoot the Missile and it definitely creates the explosion, it doesn't have any effect on the Cubes. I tried to raise power and radius, but it didn't affect the cube.
I don't have an Idea what else I could try...
Hope someone could help me.
Answer by robertbu · Jul 18, 2013 at 07:01 PM
Your error is on line 11/12. You pulled this code out of the reference, but you left out a line. It should be:
if (!hit)
continue;
That's written in the Code example for Javascript. Does it work for C#?
Okay, that's actually correct, so I would suggest Unity to Update their Scripting reference.
Thank you
Your answer
Follow this Question
Related Questions
AddExplosionForce 0 Answers
Problem with AddExplosionForce / Exploding 1 Answer
Split a object and make another one appears 1 Answer
OnCollisionEnter problem (RocketJump script) 1 Answer
Instantiated Bomb Not Exploding 1 Answer