Unity:将ExplosionForce添加到2D

本文关键字:2D 添加 ExplosionForce Unity | 更新日期: 2023-09-27 18:26:33

我想将我的脚本更改为2D c#脚本。我知道AddExplosionForce不是UnityEngine.Rigidbody2D的成员,所以我如何将其更改为2D脚本,并且仍然对所有方向施加相同的力。(基本上是一样的,但在2D中)谢谢!这是我的脚本:

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

Unity:将ExplosionForce添加到2D

我不知道内置了什么,但它实际上很容易实现。下面是一个使用扩展方法的例子:

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

现在你可以像使用3D刚体AddExplosionForce一样简单地使用它,例如使用你的代码:

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

请参阅演示:https://dl.dropboxusercontent.com/u/16950335/Explosion/index.html

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

最初编写此代码的人做得很好,但唯一的问题是代码没有利用爆炸半径。所以我对代码做了一些修改,爆炸效果非常好。所以现在,根据爆炸点到刚体的距离,将根据距离施加不同的力。

这是我的代码版本:

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

关于这个问题的答案很好,但对我来说不起作用。那是在他使用的Forcemode2D中。据我所知,Forcemode2D.Force只适用于长时间执行的力。然而,例如,爆炸效果更像。跳跃。所以我只是把Forcemode2D.Force改成了Forcemode2D.Impulse,现在它可以工作了^^

这是新的代码

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

无论如何,谢谢你o/