I am just starting in JavaScript and Unity. I came acrost this code in the FPS_tutorial. It finds all the RididBodys near a collision and blasts them away from collision. Here is the code.
//Find all nearby colliders
var colliders : Collider[] = Physics.OverlapSphere( transform.position,
explosionRadius );
//Apply a force to all surrounding rigid bodies.
**for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody**.AddExplosionForce( explosionPower,
transform.position, explosionRadius );
}
}
//If we have a particle emitter attached, emit particles for .5 seconds
if( particleEmitter )
{
particleEmitter.emit = true;
yield WaitForSeconds( 0.5 );
particleEmitter.emit = false;
}
The part I do not understand has two stars around it(which I added). What is the "for()" doing? Is it like a "if()"? Why do I not see the "hit in colliders" variable anywhere but in the the "for()"? What is asked in the if statement "if(hit.rigidbody)"? Is "hit" a list of things that have collided with something? Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit? Please don't assume I know anything about Unity or JavaScript in your answers.
Clean your code, so it is easier to follow.
for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
explosionRadius );
}
}
What is the "for()" doing?
It loops through every collider in colliders.
The variable hit is for every loop assigned to the next collider.
It is the same as:
for( var i = 0; i < colliders.Length; ++i )
{
var hit = colliders*; // This is your "var hit in colliders"*
*if( hit.rigidbody )*
*{*
*hit.rigidbody.AddExplosionForce( explosionPower, transform.position,*
*explosionRadius );*
*}*
*}*
*```*
*<blockquote>*
*<p>Is it like a "if()"?</p>*
*</blockquote>*
*<p>No, but it does have a scope (the { and }) so it looks very much like an if. The for will repeat the code inside the scope for every item in your collection.</p>*
*<blockquote>*
*<p>What is asked in the if statement "if(hit.rigidbody)"</p>*
*</blockquote>*
*<p>It checks if the collider has a rigid body. It is the same as calling "if(hit.rigidbody != null)".</p>*
*<blockquote>*
*<p>Is "hit" a list of things that have collided with something?</p>*
*</blockquote>*
*<p>No, hit is one of the objects that you have in your collider collection. Since it is in a loop, hit changes each time the loop is run so it will repeat for every object in your collider collection. Just think that "hit" is going to be "all" your colliders, one at a time in the loop.</p>*
*<blockquote>*
*<p>Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit? </p>*
*</blockquote>*
*<p>Yes, it applies an explosion force to every rigid body that was attached to a collider in the collision test.</p>*
*<hr>*
*<p>I think you could do some simple tests to boost your confidence in what the code is doing. Try this:</p>*
*```*
*function Start()*
*{*
*// Create a collection of strings*
*var collection = new String[3];*
*// Put some data in it*
*collection[0] = "Hello";*
*collection[1] = "For";*
*collection[2] = "Loop";*
*print ("Test 1");*
*// Test the for with enumeration*
*for (var each in collection)*
*{*
*print ("each = " + each);*
*}*
*print ("Test 2");*
*// Test the for with explicit control,*
*// backwards for fun!*
*for (var i = collection.Length - 1; i >= 0; --i)*
*{*
_var each = collection*;*_
_*print ("i = " + i);*_
_*print ("each = " + each);*_
_*}*_
_*}*_
_*```*_
_*<p>The output for the first test:</p>*_
_*<blockquote>*_
_*```*_
_*Test 1*_
_*each = Hello*_
_*each = For*_
_*each = Loop*_
_*```*_
_*</blockquote>*_
_*<p>and the second test:</p>*_
_*<blockquote>*_
_*```*_
_*Test 2*_
_*i = 2*_
_*each = Loop*_
_*i = 1*_
_*each = For*_
_*i = 0*_
_*each = Hello*_
_*```*_
_*</blockquote>*_