wasicool2
wasicool2

Reputation: 193

Unity: AddExplosionForce to 2D

I want to change my script into a 2D c# script. I know that AddExplosionForce is not a member of UnityEngine.Rigidbody2D so how can I change this in to a 2D script and still have the same force applied fro all directions. (basically do the same but in 2D) Thank you! Here's my script:

#  pragma strict

var explosionStrength : float = 100;

 function OnCollisionEnter(_other: Collision) 
{
if (_other.collider.gameObject.name == "Bouncy object")
   _other.rigidbody.AddExplosionForce(explosionStrength, this.transform.position,5);
}

Upvotes: 5

Views: 13346

Answers (3)

JacobRGames
JacobRGames

Reputation: 21

The person who originally made this code did a good job, but the only problem is that the code does not utilise the explosion radius. So I made some modifications to the code, and the explosion works perfectly. So now, depending on the distance from the explosion point to the rigid body, different forces will be applied based on distance.

Here is my version of the code:

public static void AddExplosionForce(this Rigidbody2D rb, float explosionForce, Vector2 explosionPosition, float explosionRadius, float upwardsModifier = 0.0F, ForceMode2D mode = ForceMode2D.Force)
{
    var explosionDir = rb.position - explosionPosition;
    var explosionDistance = (explosionDir.magnitude / explosionRadius);

    // Normalize without computing magnitude again
    if (upwardsModifier == 0)
    {
        explosionDir /= explosionDistance;
    }
    else
    {
        // If you pass a non-zero value for the upwardsModifier parameter, the direction
        // will be modified by subtracting that value from the Y component of the centre point.
        explosionDir.y += upwardsModifier;
        explosionDir.Normalize();
    }

    rb.AddForce(Mathf.Lerp(0, explosionForce, (1 - explosionDistance)) * explosionDir, mode);
}

Upvotes: 2

Alexander Braun
Alexander Braun

Reputation: 11

The answer about this is pretty good, however, it didn't work for me. That was in the Forcemode2D he used. Forcemode2D.Force is as far as I know only for force that has been performed over a long period of time. However, the explosion effect is more like, for example. a jump. So I just changed the Forcemode2D.Force to Forcemode2D.Impulse and now it works ^^

Heres the new code

using UnityEngine;

public static class Rigidbody2DExt {

    public static void AddExplosionForce(this Rigidbody2D rb, float explosionForce, Vector2 explosionPosition, float explosionRadius, float upwardsModifier = 0.0F, ForceMode2D mode = ForceMode2D.Force) {
        var explosionDir = rb.position - explosionPosition;
        var explosionDistance = explosionDir.magnitude;

        // Normalize without computing magnitude again
        if (upwardsModifier == 0)
            explosionDir /= explosionDistance;
        else {
            // From Rigidbody.AddExplosionForce doc:
            // If you pass a non-zero value for the upwardsModifier parameter, the direction
            // will be modified by subtracting that value from the Y component of the centre point.
            explosionDir.y += upwardsModifier;
            explosionDir.Normalize();
        }

        rb.AddForce(Mathf.Lerp(0, explosionForce, (1 - explosionDistance)) * explosionDir, mode);
    }
}

Anyway thank you o /

Upvotes: 1

Yuri Nudelman
Yuri Nudelman

Reputation: 2943

There is nothing built in I know of, but it is actually quite easy to implement. Here is an example using extension method:

using UnityEngine;

public static class Rigidbody2DExt {

    public static void AddExplosionForce(this Rigidbody2D rb, float explosionForce, Vector2 explosionPosition, float explosionRadius, float upwardsModifier = 0.0F, ForceMode2D mode = ForceMode2D.Force) {
        var explosionDir = rb.position - explosionPosition;
        var explosionDistance = explosionDir.magnitude;

        // Normalize without computing magnitude again
        if (upwardsModifier == 0)
            explosionDir /= explosionDistance;
        else {
            // From Rigidbody.AddExplosionForce doc:
            // If you pass a non-zero value for the upwardsModifier parameter, the direction
            // will be modified by subtracting that value from the Y component of the centre point.
            explosionDir.y += upwardsModifier;
            explosionDir.Normalize();
        }

        rb.AddForce(Mathf.Lerp(0, explosionForce, (1 - explosionDistance)) * explosionDir, mode);
    }
}

Now you can simply use it as you would use 3D rigidbody AddExplosionForce, for example with your code:

public class Test : MonoBehaviour {
    public float explosionStrength  = 100;

    void OnCollisionEnter2D( Collision2D _other) 
    {
        if (_other.collider.gameObject.name == "Bouncy object")
            _other.rigidbody.AddExplosionForce(explosionStrength, this.transform.position,5);
    }
}

See demo: https://dl.dropboxusercontent.com/u/16950335/Explosion/index.html

Source: https://dl.dropboxusercontent.com/u/16950335/Explosion/AddExplosionForce2D.zip

Upvotes: 7

Related Questions