Search code examples
unity-game-enginecollisionunityscriptflappy-bird-clone

Why my Players life disappear at once?


I am new in unity and I am developing game like Flippy Bird where if my player is collide with another object then one life of my player will disable and the collider is travel from one place to another place and there is so many colliders and the collider is little bit wide so when my player touch with that collider then its three life is disabled one by one in very short time.

I understand that when my player touch the same object again and again. So that players all life cut at the same time but i don't know how to solve this.

Code for Collision is below.

void OnTriggerEnter(Collider C){
    if(C.gameObject.name == "DownCollider" || C.gameObject.name == "Cylinder"){
        if(Life1.activeInHierarchy){
            Life1.SetActive(false);
        }
        else if(Life2.activeInHierarchy){
            Life2.SetActive(false);
        }
        else if(Life3.activeInHierarchy){
            Life3.SetActive(false);

        }
    }

Solution

  • I just try this and it works for me..

        void Update () {
    
            Vector3 pos = transform.position;
            pos.y -= 0.03f;
            transform.localRotation = Quaternion.Euler(60,270,90);
            transform.position = pos;
            if(Input.GetMouseButton(0)){
                transform.localRotation = Quaternion.Euler(72,90,-90);
                pos.y += 0.1f;
                transform.position = pos;
            }
            LifeT -= Time.deltaTime;
    
            if(LifeT <= 0){
                LifeTime = true;
                LifeT = 5f;
            }
        }
        void OnTriggerEnter(Collider C){
            if(C.gameObject.name == "DownCollider" || C.gameObject.name == "Cylinder"){
                print(LifeTime);
                if(Life1.activeInHierarchy && LifeTime){
                    Life1.SetActive(false);
                    LifeTime = false;
                }
                else if(Life2.activeInHierarchy && LifeTime){
                    Life2.SetActive(false);
                    LifeTime = false;
                }
                else if(Life3.activeInHierarchy && LifeTime){
                    Life3.SetActive(false);
                    LifeTime = false;
                }
            }
    

    Thanks for help KennethLJJ.

    Regards,

    Dharmesh