- Home /
How to initiated floors to fall like dominos once the game starts
first off I am really new to using Unity3d and very new to the scripting aspect. I Basically just started a week ago. But I have an idea and would like to try and test it out. I do have this script that i got to initiate the two Planes i made to fall as soon as the game starts.
// Apply a downward force to the rigid body every frame
function FixedUpdate () {
rigidbody.AddForce (Vector3.down);
}
I know I need a trigger with a time frame, but i don't know how to code it. I wanted to add a timer to lets say Plane A to trigger an start to fall in the first second then Plane B to trigger another second after it, then the next Plane etc etc. So basically making the floors fall in segments withing a certain time frame.
As of right now i start the game and both planes A & B start to fall at the same time. I know i need functions for this but i don't know where to start scripting it. Anyone have any ideas or places they can guide me to accomplish this task?
Answer by seealsoidiot · Jan 23, 2012 at 01:49 PM
Would just adding a rigidbody component to the planes not give you your desired effect?
Answer by Bicko · Jan 23, 2012 at 11:18 PM
I thought I'd have a look at what other questions you've got on your account, so here's my suggestion for this one.
What you want to do is loop repeatedly through all of the game objects, making a domino fall each time the loop happens, but you also want the loop to work on a delay between each domino.
First, you'll need to setup the dominoes. Give them a collider (Box Collider, probably) and as seealsoidiot said also add a Rigidbody component. You can find both of these in Component > Physics.
Now running the loop can be done in a couple of ways, but I'm going to keep things simple and suggest that you just make an empty GameObject and call it "Domino Parent", put all your dominoes inside this object.
What I've now done is made this script for you, I'd suggest you rework it in some manner, but it will just work if you make a new script, paste this inside, and put it on any object in your scene:
//The domino parent variable that contains all the other dominoes
private var dominoParent : Transform;
function Start()
{
//Set the domino parent variable on Start
dominoParent = GameObject.Find("Domino Parent").GetComponent(Transform);
//Go through each child of the object that this script is attached to,
//and set their rigidbody Kinematic component to true.
for (var child : Transform in dominoParent)
{
if(child.GetComponent(Rigidbody))
child.rigidbody.isKinematic = true;
}
//Start dropping dominoes
DropDominoes();
}
function DropDominoes()
{
//Go through each child of the object that this script is attached to,
//and set their rigidbody Kinematic component to false. Also add a small
//force to the child to wake it up.
for (var child : Transform in dominoParent)
{
if(child.GetComponent(Rigidbody))
{
child.rigidbody.isKinematic = false;
child.rigidbody.AddForce(Vector3.down);
}
//Add a 1 second delay between each domino falling.
yield WaitForSeconds(1.0);
}
}
Suggested reading: Accessing Other Game Objects, specifically the parts related to accessing children (note: all work involving parent/child game object relationships involves some dodgy wording!). Also read up on Physics Components, specifically Kinematic Rigidbodies.
Your answer
Follow this Question
Related Questions
Making a countdown out of 3D planes. 1 Answer
How to repeat damage 1 Answer
close game in delay after pressing button 1 Answer
Enable/Disable script with timer 1 Answer
Need help with game points and trigger 2 Answers