Rigidbody.AddExplosionForce

切换到手册
public void AddExplosionForce (float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier= 0.0f, ForceMode mode= ForceMode.Force));

参数

explosionForce爆炸力(可以根据距离进行修改)。
explosionPosition表示爆炸波及范围的球体的中心。
explosionRadius表示爆炸波及范围的球体的半径。
upwardsModifier调整爆炸的视位,呈现掀起物体的效果。
mode用于将力施加到其目标的方法。

描述

向模拟爆炸效果的刚体施加力。

爆炸被建模为一个在世界空间中具有特定中心位置和半径的球体;通常情况下,球体外的任何对象都不会受到爆炸的影响,力与到中心的距离成反比。但是,如果传递零值作为半径,则不管刚体距离中心多远,都将施加全部的力。

此函数在刚体表面上最接近于 explosionPosition 的点处向对象施加力。该力沿着从 explosionPosition 到刚体表面点的方向作用。如果 explosionPosition 处于刚体内,或是刚体没有活动碰撞体,则使用质心而不是表面上最接近的点。

The magnitude of the force is dependent on the distance between explosionPosition and the point where the force was applied at. As the distance between explosionPosition and the force point increases, the actual applied force decreases.

The vertical direction of the force can be modified using upwardsModifier. If this parameter is greater than zero, the force is going to be applied at the point on the surface of the rigidbody that is closest to explosionPosition shifted negative upwardsModifier along Y. 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, AddExplosionForce has no effect.

using UnityEngine;
using System.Collections;

// Applies an explosion force to all nearby rigidbodies public class ExampleClass : MonoBehaviour { public float radius = 5.0F; public float power = 10.0F;

void Start() { Vector3 explosionPos = transform.position; Collider[] colliders = Physics.OverlapSphere(explosionPos, radius); foreach (Collider hit in colliders) { Rigidbody rb = hit.GetComponent<Rigidbody>();

if (rb != null) rb.AddExplosionForce(power, explosionPos, radius, 3.0F); } } }