In my game, I need to know when all movement stops, Then check if the Good Cube fell off, or the Red Cube did. What i have now is when the red cube falls, it checks its tag for “red” and green cube is “green”. If it is green, you lose so “You Lost” Appears with a reset button. If it is red, You won so a “Next Level” button appears. But if both fall, both GUI elements appear. The thing is, I need all the movement to finish because if i do Time.timeScale, the red/green cube could be falling and it wouldnt count. SO, How do i check when all movement stops, THEN check for tags?
Your main game manager needs to know about the blocks that move and periodically check them for notmovingness.
GameManager.js
var blocks : Array;
var cutoffVelocity : float = 0.01;
function Start() {
blocks = ( get all block GameObjects from scene );
}
function Update() {
if ( Check() ) {
// blocks have stopped moving
}
}
function Check() : boolean {
var stopped : boolean = true;
for ( var block in blocks ) {
if ( block.rigidbody.velocity.magnitude > cutoffVelocity
|| block.rigidbody.angularVelocity.magnitude > cutoffVelocity ) {
stopped = false;
break;
}
}
return stopped;
}
Ok, So now how would i implement this into it.
CheckColor.js
var mySkin: GUISkin;
var fail;
var win;
function Start(){
fail = false;
win = false;
Time.timeScale = 1;
}
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "red") {
Game.RedLeft -= 1;
}
if (collision.gameObject.tag == "green") {
Game.GreenLeft -= 1;
}
}
function OnGUI(){
if ( Game.RedLeft <= 0 ){
GUI.skin = mySkin;
if (GUI.Button (Rect (0,200,80,20), "Continue")) {
Application.LoadLevel("Tutorial 3");
}
}
if ( Game.GreenLeft <= 0 ){
GUI.skin = mySkin;
if (GUI.Button (Rect (50,200,80,20), "Failed")) {
Application.LoadLevel("Tutorial 3");
}
}
}
That checks what color collides with the ground, Tagged “floor”
Destroy on Collision.js
unction OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "floor") {
Destroy(gameObject);
}
}
Thats Used to delete the cube when it falls on the ground. so it does not keep registering
Try doing
if ( Game.greenLeft < 1 ) {
// lose
} else if ( Game.redLeft < 1 ) {
// win
Then you won’t get both gui elements since it’ll only show the WIN dialogue if you haven’t LOST.
yeah but then they can have the green square fall off, loose then have the red one fall off and win.
Heres the game. Try it and tell me the best way to check the colors. I need it so it cant be like, the green one is falling, but the red one hit first, so they would win.
Play in 600 x 450 resolution to see buttons
No they can’t, not unless you set greenLeft back above 1. The code will always get into Game.greenLeft < 1 and will never have a chance to win since it’s an if-else.


