unity-game-engineflappy-bird-clone

how create function for flappy bird game?


I want to know how to create a function in c# which is called when my bird gameobject touches the ground or top of the screen. I want it to call game over when this happens. I've tried comparing bird.position.x with Screen height but it didnt work, I hope you have another way to do it. Thanks


Solution

  • The function you are looking for is called OnTriggerEnter2D

    void OnTriggerEnter2D(Collider other) {
             if(other.gameObject.tag == "Floor")
                  //You lost
     }
    

    You need to place this function in a Scrip in the bird gameobject, then add colliders to the bird and the floor, and assign a tag to the floor so you can check inside the OnTriggerEnter2D is the bird crashed against the floor.