Rigidbody.ClosestPointOnBounds 到包围盒的最近点
public class example : MonoBehaviour { function Start () { var explosionPosition = transform.position;//本脚本依附在爆炸火焰prefabs上 var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);//获取以爆炸点为中心,一定半径内的球体内的GameObect //遍历范围内的对象 for (var hit in colliders) { if (!hit) continue; //如果这个GameObject是刚体 if (hit.rigidbody) { hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);//给这个刚体一个外力 var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);//取出附近的这个物体最接近爆炸点的点,到爆炸点的距离 var distance = Vector3.Distance(closestPoint, explosionPosition);//计算出距离 var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);// Mathf.Clamp01表示把数值强制在0..1范围之内,如果数值超过这个范围,则取上限或下线 hitPoints *= explosionDamage; hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver); } } }
这个js代码,可以用上次讲的转代码功能转成c#理解哦~
适用于打中油桶,油桶爆炸范围内造成的伤害. 距离不一样,伤害也不一样~
时间: 2024-10-08 20:12:20