I'm trying to implement flappy bird clone using unity. I'm creating two obstacles at random y position. I would like to create a hidden box collider game object between them, but I'm struggling with the math. I want the box collider to be generated between both game objects so that I use it to detect if the bird has passed the two obstacles.

Here is how I generate the obstacles

// function called by InvokeRepeating function

void CreateObstacle(){
        // generating random upper pipe position
        float randomPos = 4f-(4f-0.8f-pipeHole)*Random.value;
        // adding upper pipe to stage
        GameObject upperPipe = (GameObject)Instantiate(pipeObject);
        // setting upper pipe position
        upperPipe.transform.position = new Vector2(4f,randomPos);
        // adding lower pipe to stage
        GameObject lowerPipe = (GameObject)Instantiate(pipeObject);
        // setting lower pipe position
        lowerPipe.transform.position = new Vector2(4f,randomPos-pipeHole-4.8f);
  • \$\begingroup\$ What do you mean by "hidden" box collider? \$\endgroup\$ – Pip Jul 2 '15 at 12:42
  • \$\begingroup\$ a game object that is not seen, its just acting as a box collider \$\endgroup\$ – user3023213 Jul 2 '15 at 12:43
  • \$\begingroup\$ Make a prefab that holds your empty gameobject + collider (+ a script probably). Middle position will be (upperPipe.transform.position + lowerPipe.transform.postion) / 2 . Then scale your trigger object based on the difference in position: (upperPipe.transform.position - lowerPipe.transform.postion). \$\endgroup\$ – MistaGiggles Jul 2 '15 at 12:57
  • \$\begingroup\$ scale it in which axis ? \$\endgroup\$ – user3023213 Jul 2 '15 at 12:59
  • \$\begingroup\$ Depends on which axis your pipes are aligned on. The difference in position will have a value of 0 for each axis that are aligned, you won't want to scale these. Scale it on each axis that has a difference != 0 (for example, try scale += difference/2 , and if you are using default scaling for everything the math might just work in your favor) \$\endgroup\$ – MistaGiggles Jul 2 '15 at 15:21

why not make a prefab with 3 gameobjects with a trigger in the middle and you spawn the complete prefab should be much easier


You need to create emtpy gameobject and add colliders to it. Also, check the istrigger? checkbox. because, a normal collider will not allow the bird to pass through

  • \$\begingroup\$ what are the math \$\endgroup\$ – user3023213 Jul 2 '15 at 13:55
  • \$\begingroup\$ No maths. You just set it trigger. Then, to update score after the bird has crossed it, refer to OnTriggerExit2D() \$\endgroup\$ – SanSolo Jul 2 '15 at 13:59

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.