Rigidbody.closestpointonbounds to the nearest point of the bounding box
public class Example:monobehaviour {function Start () {var explosionposition = transform.position;//This script is attached to the exploded flame prefabs var colliders:collider[] = Physics.overlapsphere (explosionposition, Explosionradius);//Get the explosion point as the center, Within a certain radius of the sphere inside the Gameobect//traverse range of the object for (Var hits in colliders) {if (!hit) continue;//If this gameobject is a rigid body if (hit.rigidbody) {h It.rigidbody.AddExplosionForce (Explosionpower, Explosionposition, Explosionradius, 3.0);//give this rigid body an external var Closestpoint = Hit.rigidbody.ClosestPointOnBounds (explosionposition);//Remove the nearby object closest to the point of the explosion, the distance to the explosion point var distance = Vector3.distance (Closestpoint, explosionposition);//Calculate the distance var hitpoints = 1.0-mathf.clamp01 (Distance/ Explosionradius);//MATHF.CLAMP01 means to force the value at 0. 1 range, if the value exceeds this range, then take upper limit or downline hitpoints *= explosiondamage; Hit.rigidbody.SendMessageUpwards ("Applydamage", hitpoints, Sendmessageoptions.dontrequirereceiver); } }}
This JS code, you can use the previous transcoding function to C # understanding Oh ~
It is suitable for the damage caused by the explosion of oil barrels and barrels. The distance is different, the damage is not the same ~
Unity Day A technical point (vii)---to the nearest point of the bounding box