- Home /
Delay something until no object has a certain condition
I don't want the game to advance a level if an object is falling , I already have something on each object that detects this an "isFalling" boolean it goes to false if it doesnt fall simple enough but what would be a good way of checking if any of them is falling, they are all the same objects but what would be a non-expensive way to check wether one is falling and then I want to delay the execution of other code as in make it wait till nothing is falling anymore what would be a good way of doing this?
Thanks,
Jan Julius.
Are they instantiated at runtime or they was defined in scene?
Every time you have an object that starts falling, increment a static int. Each time an object stops falling, decrement that same static int. When the static int gets to zero, you know the level can advance.
They are instantiated and richyrich I was thinking about this but the objects can get destroyed and new ones instantiate, they can also fall when other ones get destroyed and the level can't end whenever one is falling.
Answer by fafase · Nov 03, 2014 at 10:03 AM
Keep track of all rigidbodies in your game.
private List<Rigidbody>rigList = new List<Rigidbody>();
void Start()
{
var rig = FindObjectsOfType<Rigidbody>();
foreach(Rigidbody r in rig)
{
rigList.Add(r);
}
}
Now you got all rigidbody at start.
Any time you add a new rigidbody to the game add it to the list, and also remove it when you destroy one.
Finally, when you should be transiting, check if the velocity is 0:
bool CheckMovingRigidbody()
{
for(int i = rigList.Count - 1; i >=0; i--)
{
if(rigList[i].velocity.magnitude < 0.01f)
{
rigList.RemoveAt(i);
}
}
if(rigList.Count == 0)
{
return true;
}
return false;
}
You use this method to check if all rigidbodies are not moving, if the movement is small enough, you remove the item from the list and you try again later.
Note that this considers that once a rigidbody is not moving, it won't get moving again since it gets removed from the list and won't get back in.
EDIT: since you say you have all objects to fall until they are told not to apply the same process but instead of getting the rigidbody and checking the velocity, you grab the script where they are told to move and set a boolean there and set it to false when not moving:
public class Movement:MonoBehaviour{
private bool isMoving = false;
public void MoveObject(){
// Some code
isMoving = true;
}
public void StopMoving(){
isMoving = false;
}
}
I am not using rigidbodies for this though, the objects are just constantly transfor$$anonymous$$g down until my script says they can't anymore.
Does a rigidbody apply velocity when it's being transformed and gravity is turned off?
Answer by SteveBC · Nov 03, 2014 at 11:16 AM
Try saving them all in an array or list of your objects (make it an array/list of the Class with the isFalling variable to avoid unnecessary GetComponents and check for the value of isFalling of each one in a for-loop.
YourClass[] objects = new YourClass[5];
for(int i=0; i<objects.Length; i++) {
if(objects[i].isFalling) {
//Do fun stuff
}
}
Please read other answers first. I understand you want to help but you do not provide more info that I did.
Actually, looking at the timestamp on both answers, this one was first. It was stuck in the moderation queue. Just treat as a simultaneous post. Upvotes for everybody.