- Home /
4 blocks destroyed - gravity true
Lads, I'm shooting projectiles at a tower of blocks. When a block is struck it is destroyed. Once 4 blocks are destroyed I'd like to turn on gravity for ALL the blocks so the tower will crumble.
Any ideas on how to script something like this?
Thx
Stef
Answer by rutter · Mar 07, 2012 at 11:00 PM
Assuming you have a Rigidbody attached to each block, you could set their useGravity flags to true, once the tower should collapse.
Thanks, but I'm new to scripting and was hoping for some help on writing the script.
Ah, I see. The part you'll probably find the most challenging is getting a reference to each block's Rigidbody -- once you have the reference, setting the flag is easy enough.
Let's suppose all of those blocks are parented to a particular GameObject. From a script on that parent object, we could do something like this:
for (var R : Rigidbody in GetComponentsInChildren(Rigidbody)) { R.useGravity = true; }
Or, if each block is tagged with "Block", we could find them that way:
for (var O in GameObject.FindGameObjectsWithTag("Block")) { if ( O.rigidbody != null ) { O.rigidbody.useGravity = true; } }
Is that what you're having trouble with, or is there something else? It'll be much easier to answer more specific questions.
Thanks for your help rutter. I'm trying this (it's not working), what do you think?:
int blocksDestroyed = 0;
public void IncrementBlocksDestroyed ()
{
if (++blocksDestroyed == 4);
{
then rigidbody.useGravity = true;
}
}
Answer by Stef · Mar 09, 2012 at 10:12 PM
Thanks for the feedback. I'm trying this but not having much success: var smokePrefab: Transform; var int objectDestroyed = 0;
//Invoke("OnCollisionEnter", 2);
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "missile_1")
{
Destroy (gameObject);
Instantiate (smokePrefab, transform.position, transform.rotation);
++objectDestroyed;
}
{
if objectDestroyed == 4;
{
gameObject.rigidbody.useGravity = true;
}
}
Any suggestions?...
Your answer
Follow this Question
Related Questions
Detecting when certain objects have been destroyed 2 Answers
Keeping score when object clicked and destroyed 2 Answers
count score start 0 2 Answers
How to keep score? 1 Answer
script syntax 1 Answer