最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unity: AddExplosionForce to 2D - Stack Overflow

programmeradmin2浏览0评论

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

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);
}
Share Improve this question asked Dec 13, 2015 at 12:03 wasicool2wasicool2 1931 gold badge4 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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 puting 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 ponent 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./u/16950335/Explosion/index.html

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

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 puting 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 ponent of the centre point.
        explosionDir.y += upwardsModifier;
        explosionDir.Normalize();
    }

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

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 puting 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 ponent of the centre point.
            explosionDir.y += upwardsModifier;
            explosionDir.Normalize();
        }

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

Anyway thank you o /

发布评论

评论列表(0)

  1. 暂无评论