Answers for "unity explosion 3d"

C#
1

explosion physics in unity

AddExplosionForce(float explosionForce, 
				Vector3 explosionPosition, 
                float explosionRadius, 
                float upwardsModifier = 0.0f, 
                ForceMode mode = ForceMode.Force));

// Example
public float power = 10.0F;
Vector3 explosionPos = transform.position;
public float radius = 5.0F;

Rigidbody rb = GetComponent<Rigidbody>();
if (rb != null) // Check if object have component "Rigidbody"
	rb.AddExplosionForce(power, explosionPos, radius, 3.0F);
Posted by: Guest on May-21-2021
0

2d explosion unity

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);
    }
}
Posted by: Guest on June-01-2021

C# Answers by Framework

Browse Popular Code Answers by Language