AddExplosionForce(float, Vector3, float, float, ForceMode)
Applies a force to a rigidbody that simulates explosion effects.
Read time 2 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.PhysicsModule
public void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier, ForceMode mode)
Parameters
The force of the explosion (which may be modified by distance).
The centre of the sphere within which the explosion has its effect.
The radius of the sphere within which the explosion has its effect.
Adjustment to the apparent position of the explosion to make it seem to lift objects.
The method used to apply the force to its targets.
Remarks
The explosion is modelled as a sphere with a certain centre position and radius in world space; normally, anything outside the sphere is not affected by the explosion and the force decreases in proportion to distance from the centre. However, if a value of zero is passed for the radius then the full force will be applied regardless of how far the centre is from the rigidbody.
This function applies a force to the object at the point on the surface of the rigidbody that is closest to . The force acts along the direction from to the surface point on the rigidbody. If is inside the rigidbody, or the rigidbody has no active colliders, then the center of mass is used instead of the closest point on the surface.
explosionPositionexplosionPositionexplosionPositionThe magnitude of the force depends on the distance between and the point where the force was applied. As the distance between and the force point increases, the actual applied force decreases.
explosionPositionexplosionPositionThe vertical direction of the force can be modified using . If this parameter is greater than zero, the force is applied at the point on the surface of the Rigidbody that is closest to but shifted along the y-axis by negative . Using this parameter, you can make the explosion appear to throw objects up into the air, which can give a more dramatic effect rather than a simple outward force. Force can be applied only to an active rigidbody. If a GameObject is inactive, has no effect.
upwardsModifierexplosionPositionupwardsModifierAddExplosionForceNote: If you are using or , the force applied by is scaled by the world-space inertia tensor matrix.
ForceMode.AccelerationForceMode.VelocityChangeAddExplosionForceAdditional Resources: _inertiaTensor
Examples
using UnityEngine;public class Example : MonoBehaviour{ [Header("Explosion Settings")] public float radius = 5.0f; public float power = 10.0f; public float upwardsModifier = 3.0f; public ForceMode forceMode = ForceMode.Force; [Header("Optional Settings")] public LayerMask affectedLayers = ~0; // All layers by default void Update() { ApplyExplosionForce(); } public void ApplyExplosionForce() { Vector3 explosionPosition = transform.position; Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius, affectedLayers); foreach (Collider collider in colliders) { if (collider.TryGetComponent<Rigidbody>(out Rigidbody rb)) { rb.AddExplosionForce(power, explosionPosition, radius, upwardsModifier, forceMode); } } } void OnDrawGizmosSelected() { Gizmos.color = new Color(1f, 0.5f, 0f, 0.5f); Gizmos.DrawWireSphere(transform.position, radius); }}