返回介绍

Rigidbody.AddExplosionForce 添加爆炸力

发布于 2019-12-18 15:38:28 字数 4802 浏览 1659 评论 0 收藏 0

JavaScript => public function AddExplosionForce(explosionForce: float, explosionPosition: Vector3, explosionRadius: float, upwardsModifier: float = 0.0F, mode: ForceMode = ForceMode.Force): void;

C# => public void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier = 0.0F, ForceMode mode = ForceMode.Force);

Parameters 参数

explosionForceThe force of the explosion (which may be modified by distance).
爆炸力,可以通过距离来修改
explosionPositionThe centre of the sphere within which the explosion has its effect.
球体的中心。
explosionRadiusThe radius of the sphere within which the explosion has its effect.
球体的半径,在半径内才有爆炸效果。
upwardsModifierAdjustment to the apparent position of the explosion to make it seem to lift objects.
调节爆炸出现的位置,使其看起来像掀起对象。
modeThe method used to apply the force to its targets.
用于应用到目标力的方式。

Description 描述

Applies a force to a rigidbody that simulates explosion effects.

应用一个力到刚体来模拟爆炸效果。

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.

爆炸是一个具有中心点和半径的球体,在世界坐标空间;一般情况下,在球体外的任何东西都不会受到爆炸的影响并且力从中心到外按比例减小。然而,如果半径为0,那么力完全应用到刚体,而不管距离中心有多远。

By default, the direction of the force is the line going from the explosion centre to the rigidbody's centre of mass. 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. For example, if you pass a value of 2.0 for upwardsModifier, the explosion will appear to be centred 2.0 units below its actual position for purposes of calculating the force direction (ie, the centre and the radius of effect are not modified). Using this parameter, you can easily make the explosion appear to throw objects up into the air, which often gives a more dramatic effect than a simple outward force.

默认,从爆炸中心到刚体的质量中心力的方向是线性。如果你upwardsModifier参数传递非0值,该方向将通过减去中心点Y轴的值修改。例如,如果upwardsModifier的值为2.0,那么该爆炸出现在实际位置中心点2单位下(如,中心和影响的半径不会被修改)。使用这个参数,你可以很容易地使爆炸似乎把物体扔到空中,这往往比单纯的外力更具戏剧性的效果。

Force can be applied only to an active rigidbody. If a GameObject is inactive, AddExplosionForce has no effect.

力仅应用于激活的刚体。如果游戏对象未激活,AddExplosionForce也将无效。

JavaScript:

#pragma strict
// Applies an explosion force to all nearby rigidbodies
public var radius: float = 5.0F;
public var power: float = 10.0F;
function Start() {
	var explosionPos: Vector3 = transform.position;
	var colliders: Collider[] = Physics.OverlapSphere(explosionPos, radius);
	for (var hit: Collider in colliders) {
		var rb: Rigidbody = hit.GetComponent.<Rigidbody>();
		if (rb != null)
			rb.AddExplosionForce(power, explosionPos, radius, 3.0F);
	}
}

C#:

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);
 
        }
    }
}

Rigidbody

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文