explosionForce | 爆炸力(可以根据距离进行修改)。 |
explosionPosition | 表示爆炸波及范围的球体的中心。 |
explosionRadius | 表示爆炸波及范围的球体的半径。 |
upwardsModifier | 调整爆炸的视位,呈现掀起物体的效果。 |
mode | 用于将力施加到其目标的方法。 |
向模拟爆炸效果的刚体施加力。
爆炸被建模为一个在世界空间中具有特定中心位置和半径的球体;通常情况下,球体外的任何对象都不会受到爆炸的影响,力与到中心的距离成反比。但是,如果传递零值作为半径,则不管刚体距离中心多远,都将施加全部的力。
此函数在刚体表面上最接近于 explosionPosition
的点处向对象施加力。该力沿着从 explosionPosition
到刚体表面点的方向作用。如果 explosionPosition
处于刚体内,或是刚体没有活动碰撞体,则使用质心而不是表面上最接近的点。
力的大小取决于 explosionPosition
与施加力的点之间的距离。随着 explosionPosition
与施力点之间的距离增加,施加的实际力会减少。
可以使用 upwardsModifier
修改力的垂直方向。如果此参数大于零,则在刚体表面上最接近于 explosionPosition
的点处施加力,不过会沿 y 轴移动负的 /upwardsModifier/。使用此参数可以实现将对象抛向空中的爆炸效果,这可能比简单地施加向外的力更明显。
力只能施加于处于活动状态的刚体。如果 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.