1
\$\begingroup\$

My idea is that there is a plane which is more of a border at top of the screen and if a game object has no more space to fall and it collides with something while it is in that border, the game ends. How can I check if a gameobject is inside that plane?

Example: enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Could you please explain it little bit more? \$\endgroup\$ – Hamza Hasan Dec 30 '15 at 13:40
  • \$\begingroup\$ So you only want to react on collision events when the object is within a given zone and react them otherwise? By the way, do you know what Colliders are in Unity? \$\endgroup\$ – Philipp Dec 30 '15 at 14:58
  • \$\begingroup\$ I edited my post with a sketch example \$\endgroup\$ – Milen Dec 30 '15 at 15:11
  • \$\begingroup\$ Looks like you want a Collider volume that is marked as a trigger \$\endgroup\$ – Draco18s Dec 30 '15 at 15:11
  • \$\begingroup\$ @Draco18s Can you show how this is done? \$\endgroup\$ – Milen Dec 30 '15 at 15:30
2
\$\begingroup\$

This is a great use case for Colliders in Unity. http://docs.unity3d.com/ScriptReference/Collider.html

You would actually want to achieve your objective using 2 colliders. The first being your Border Collider, the second being the "Play Space" collider.

You would then want a script for your GameObjects (squares/rectangles) which checks their collisions, and using a few variables determine which state you are describing.

An example (psuedo code) would be something like this:

using UnityEngine;
using System.Collections;

public class falling_game_block : MonoBehaviour {

bool border_collide = false;
bool playspace_collide = false;

void OnCollisionEnter(Collision collision_info) 
{
    if(collision_info.collider.name == "border_collider_name")
    {
        border_collide = true;
    }//END IF COLLISION.COLLIDER == "BORDER_COLLIDER_HERE"

    if(collision_info.collider.name == "playspace_collider_name")
    {
        playspace_collide = true;
    }//END IF COLLISION.COLLIDER == "PLAYSPACE_COLLIDER_HERE"

    print(gameObject.name + " in contact with " + collisionInfo.collider.name);
}//END FUNCTION ONCOLLISIONENTER

void OnCollisionExit(Collision collisionInfo) {
    print(gameObject.name + " No longer in contact with " + collisionInfo.collider.name);
}

void Update() 
{
    if(border_collide == true && playspace_collide == true)
    {
        //DO stuff, explode, warn, cry, etc...
    }//END IF BORDER_COLLIDE == TRUE AND PLAYSPACE_COLLIDE == TRUE
}
}//END CLASS FALLING_GAME_BLOCK

You'll want to look at the "console" to see the print messages. Comment them out when you understand what is going on.

*** Note this is just psuedo code, and is not fully tested... you may have to perform slight tweaks to address errors, and a few more tweaks to get it to handle your exact need, but this should provide a good starting point!!!

\$\endgroup\$
0
\$\begingroup\$

Based on @Danoweb's answer, here is how I did it:

public class CubeInteraction : MonoBehaviour {
    private bool endGameBorderCollided;
    private bool cubeCollided;

    private void Update() {
    //Check if there is any space left for cubes to fall. If not - end the game
    CheckForCubeOverflow();

    // Controls touch inputs by the user
    RegisterTouchInputs();
    }

    // Check if a cube enters the end game border and if it collides with another cube at that time
    private void CheckForCubeOverflow() {
       if (endGameBorderCollided && cubeCollided)
           gameOverController.EndGame();
    }

    private void OnTriggerEnter2D(Collider2D collider2D) {
       if (collider2D.name == "End Game Border")
           endGameBorderCollided = true;
    }

    private void OnTriggerExit2D(Collider2D collider2D) {
       if (collider2D.name == "End Game Border")
           endGameBorderCollided = false;
    }

    private void OnCollisionEnter2D(Collision2D collision2D) {
       if (collision2D.collider.name.Contains("Cube"))
           cubeCollided = true;
    }

    private void OnCollisionExit2D(Collision2D collision2D) {
       if (collision2D.collider.name.Contains("Cube"))
           cubeCollided = false;
    }
}
\$\endgroup\$
0
\$\begingroup\$

Physics2D.OverlapArea would be perfect for this use...

\$\endgroup\$
  • 1
    \$\begingroup\$ Please add more details to your answer. Why would it be perfect for this use? How should it be used? One-line answers tend to be deleted. \$\endgroup\$ – Alexandre Vaillancourt Jun 13 '16 at 12:33
  • \$\begingroup\$ This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review \$\endgroup\$ – Alexandre Vaillancourt Jun 13 '16 at 13:36
  • \$\begingroup\$ this built-in function seems like created for such cases : it checks if two colliders overlap (thats why it seemed obvious to me and needed no more explanations) place a rectangle collider in the 'game over zone' and do a check for other colliders presence on each turn. if it brings a result -> game over. \$\endgroup\$ – Denis Taquet Jun 13 '16 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.