Unity AddExplosionForce not doing anything
Posted
by
Zero
on Game Development
See other posts from Game Development
or by Zero
Published on 2013-10-19T13:26:11Z
Indexed on
2013/10/19
16:10 UTC
Read the original article
Hit count: 1012
Recently I've started learning Unity3D. I'm working on a game as an exercise in which you control a space ship and have to dodge asteroids. If you feel like it's getting a bit too much you can hit the space bar, emitting a blast in all directions that repulses nearby asteroids. To create this blast I have the following code:
public class PlayerBlastScript : MonoBehaviour {
public ParticleSystem BlastEffect;
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.Space)) {
Fire();
}
}
public void Fire() {
ParticleEmitter effect = (ParticleEmitter) Instantiate (BlastEffect, transform.position, Quaternion.identity);
effect.Emit();
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, 25.0f);
foreach(Collider hit in colliders) {
if (!hit) {
continue;
}
if (hit.rigidbody) {
hit.rigidbody.AddExplosionForce(5000.0f, explosionPos, 100.0f);
}
}
}
}
Even though the blast effect appears, the asteroids are not affected at all. The asteroids are all rigid bodies so what's the problem?
© Game Development or respective owner