- Home /
Explosion-Simple.js script of FPS Tutorial
Hi all, I'm new to Unity. I've read GUI Essentials and Scripting Essentials tutorials, and now I'm having some problems understanding a script of the FPS Tutorial Part 2.
var explosionRadius = 5.0; var explosionPower = 10.0; var explosionDamage = 100.0;
var explosionTime = 1.0;
function Start () {
var explosionPosition = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) { if (!hit) continue;
if (hit.rigidbody) {
hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
// The hit points we apply fall decrease with distance from the hit point
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
// Tell the rigidbody or any other script attached to the hit object
// how much damage is to be applied!
hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
}
}
// stop emitting ? if (particleEmitter) { particleEmitter.emit = true; yield WaitForSeconds(0.5); particleEmitter.emit = false; }
// destroy the explosion Destroy (gameObject, explosionTime);
}
In particular, I don't understand some instructions in the for-each: 1) Why there is "if (!hit) continue"? for-each should select every element in colliders, shouldn't it? 2) I don't understand these instructions: var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius); hitPoints *= explosionDamage; // Tell the rigidbody or any other script attached to the hit object // how much damage is to be applied! hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
Thank you very much for your help! :)
Answer by Julian-Glenn · Jul 19, 2010 at 02:23 PM
1) Here is what Continue is doing: If there are no "hits" then break the loop.
2) hitPoints is how much damage to apply to the object that was hit. the calculation (distance / explosionRadius) is Mathf.Clamp01 clamped to prevent a value < 0 or > 1 You then send a message to the object and its children to run the ApplyDamage() function using the value hitPoints that we calculated using the (distance / explosionRadius) formula.
I think a clearer explanation of the continue statement has nothing to do with there being no "hits". If there were no hits, the Physics.OverlapSphere would return an empty array and the for-each would have nothing to iterate over. I think you mean to say that if the returned hit collider object is null, then continue on to the next one to avoid dealing with the null object.
Thank you, now I understood better the answer to the first question! :) I still have a little doubt: distance / explosionRadius is always a value between 0 and 1. Why do we use $$anonymous$$athf.Clamp01()? Also, what's the difference between Component.Send$$anonymous$$essageUpwards() and GameObject.Send$$anonymous$$essageUpwards()? Thank you again!
@skovacs - yes you're right :)
@$$anonymous$$1kk0z90 - mathF.clamp01 creates a fraction and prevents negative numbers so if you're sure your code never results in a negative number or a number higher than 1.0 I'm not sure why it's there either. I am pretty terribad at math :) GameObject.Send$$anonymous$$essageUpwards works when attached to a gameObject like a mesh, component works when attached to a component like a rigidbody or script. It's not very clear on the Unity doc's but that's all I can figure.
Your answer
Follow this Question
Related Questions
How to make the enemy choose only one cover? 1 Answer
How do you make a ragdoll blow apart? 1 Answer
Explosive like in COD 4? 1 Answer
gun muzzle flashes 2 Answers