- Home /
2D Physics Thrower Side Scroller - Explosion Physics?
Hi,
Can I use add explosion force but constrain it to two axis for the purposes of 2D explosion physics. Or is there a better way for achieving this? What I want is to affect rigidbodies near a gameobject by applying relative force based on distance from explosion centre, and the power of the explosion. Do I put this on the projectile or instantiate a seperate explosion gameobject?
var radius = 5.0; var power = 10.0; function Start () { // Applies an explosion force to all nearby rigidbodies var explosionPos : Vector3 = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (!hit)
continue;
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
}
}
Answer by thiagobardez · Feb 27, 2014 at 07:58 PM
try this:
public static void AddExplosionForce(Rigidbody2D body, float explosionForce,
Vector3 explosionPosition, float explosionRadius)
{
var dir = (body.transform.position - explosionPosition);
float wearoff = 1 - (dir.magnitude / explosionRadius);
body.AddForce(dir.normalized * explosionForce * wearoff);
}
Looking at this code it seems if is is outside of the explosionRadius it will get attracted to the point of explosion ins$$anonymous$$d of ignored, because wearoff will go below zero.
Your answer
Follow this Question
Related Questions
AddExplosionForce only goes up 0 Answers
How to make/find animation effects? 0 Answers
Problem with AddExplosionForce / Exploding 1 Answer
AddExplosionForce 0 Answers