5

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);
}

3 Answers 3

7

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

6
  • Hi thanks for replying to answer. When I tried to attach the script to an object i keep on recieve a message that pops up and said " Can't add script behaviour Rigidbody 2DExt. The script class can't be abstract! " Thank you again
    – wasicool2
    Dec 13, 2015 at 14:30
  • also when I tried creating a new script by add a component and adding your script, I get this error: The namespace global::' already contains a definition for Rigidbody2DExt'. Thank you
    – wasicool2
    Dec 13, 2015 at 14:34
  • You cannot and also don't need to attach the script to anything, it is not MonoBehaviour. Just put it in your Assets folder, it is enough. You can think of it as a library that adds a new function to Rigidbody2D class called AddExplosionForce. Here is some more info about extension methods: msdn.microsoft.com/en-us/library/… Dec 13, 2015 at 14:48
  • thank you I created a folder in my assets window named it Rigidbody2DExt and placed the script in there. However it's not knocking back my player when i collide with an object??? Sorry im a bit of a newb , sorry for asking stupid questions. Thank you
    – wasicool2
    Dec 13, 2015 at 15:19
  • Well this is not stupid, but hard to answer because it can happen for virtually any reason. I suggest you debug this, use either MonoDevelop debugger or just add some Debug.Log prints. See if OnCollisionEnter2D is called. Then see if the code enters the if clause inside this function. If not - make sure your object has a script that handles collision attached, make sure your syntax is right (you accidentally used OnCollisionEnter or Collider instead OnCollisionEnter2D Collider2D, you have object named exactly "Bouncy object", colliders are not marked isTrigger... Dec 13, 2015 at 15:36
2

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);
}
1

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 /

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.