explosionForce | 爆炸力(可以根据距离进行修改)。 |
explosionPosition | 表示爆炸波及范围的球体的中心。 |
explosionRadius | 表示爆炸波及范围的球体的半径。 |
upwardsModifier | 调整爆炸的视位,呈现掀起物体的效果。 |
mode | 用于将力施加到其目标的方法。 |
向模拟爆炸效果的刚体施加力。
爆炸被建模为一个在世界空间中具有特定中心位置和半径的球体;通常情况下,球体外的任何对象都不会受到爆炸的影响,力与到中心的距离成反比。但是,如果传递零值作为半径,则不管刚体距离中心多远,都将施加全部的力。
默认情况下,力的方向为从爆炸中心到刚体质心的直线。如果为 upwardsModifier
参数传递非零值,则会对方向进行修改(从中心点的 Y 分量中减去该值)。例如,您向 upwardsModifier
传递了值 2.0,则爆炸看似发生在其实际位置下方 2.0 单位处,并据此计算力方向(即,不修改效果的中心和半径)。您可以使用该参数轻松实现将物体抛向空中的爆炸效果,这通常比简单地施加向外的力更明显。
力只能应用于处于活动状态的刚体。如果 GameObject 处于非活动状态,则 AddExplosionForce 没有效果。
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); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.