Rigidbody.AddExplosionForce 添加爆炸力

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

Description描述

Applies a force to the rigidbody that simulates explosion effects. The explosion force will fall off linearly with distance to the rigidbody.

应用一个力到刚体来模拟爆炸效果。爆炸力将随着到刚体的距离线形衰减。

The function also plays nicely with ragdolls. If radius is 0, the full force will be applied no matter how far away position is from the rigidbody. upwardsModifier applies the force as if it was applied from beneath the object. This is useful since explosions that throw things up instead of pushing things to the side look cooler. A value of 2 will apply a force as if it is applied from 2 meters below while not changing the actual explosion position. explosionPosition is the position from which the explosion force is to be applied. explosionRadius is the radius of the explosion. Rigidbodies further away than explosionRadius will not be affected.

这个函数也对布娃娃有很好的作用。如果radius为0,将应用全部的力不论position距离刚体多远。upwardModifier就像从物体下方施加力力。这个是非常有用的,因为爆炸将向上抛物体而不是将它们推向一边,这个看起来非常的酷。为2的值将应用一个力就好象在物体2米以下施加力,然而不会改变实际的爆炸位置。 explosionPositon是爆炸力被应用的位置。explosionRadius是爆炸的半径,超过explosionRadius距离的刚体将不会受到影响。

using UnityEngine;
using System.Collections;

public class example : 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) {
			if (!typeof(hit))

			if (hit.rigidbody)
				hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);

		}
	}
}
var radius = 5.0;
var power = 10.0;
function Start () {
	// Applies an explosion force to all nearby rigidbodies
	//应用一个爆炸力到所有刚体附近
	
	var explosionPos : Vector3 = transform.position;
	var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);

	for (var hit : Collider in colliders) {
		if (!hit)
			continue;

		if (hit.rigidbody)
			hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
	}
}
最后修改:2011年2月9日 Wednesday 21:26

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。