Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D Shmup - PowerUp system - problem

Discussion in '2D' started by Przemek_S, Sep 4, 2014.

  1. Przemek_S

    Przemek_S

    Joined:
    Aug 18, 2014
    Posts:
    9
    Hi,

    I'm trying to create 2D Shmup Game - for me and my training programming skill - I'm noob. At the moment I have simple movment system, collision, firing, scoring and Life and Shell powerUp.

    I'm trying to create powerUp system for fire but stuck here.

    Player prefab collide with powerUp prefab and should increase power of fire (more damage to enemy) but this no working and I don't have any idea why.

    Part playerMove script:

    void OnCollisionEnter2D(Collision2D coll)
    {
    if(coll.gameObject.name == "ENEMY_4")
    {
    Destroy(coll.gameObject);
    score = score + 25;
    shield = shield - 10;
    if (shield > 50)
    {
    life = life - 1;
    }
    if (shield < 50)
    {
    life = life - 10;
    }
    if (shield <0)
    {
    life = life - 20;
    }

    if (life < 0)
    {
    //Destroy(this.gameObject); // niszczy obiekt gracza
    Time.timeScale = 0; // zatrzymuje gre (zamraza ekran)

    }
    }

    if (coll.gameObject.name == "1UP") -- power up prefab for shield and life - this work great
    {
    Destroy(coll.gameObject);
    shield = shield + (int)(shield*0.5f);
    life = life + (int)(life * 0.5f);
    }

    if (coll.gameObject.name == "POWUP")
    {
    Destroy(coll.gameObject);
    powup = powup + 25; -- for test and this work powUp increase when i collide with prefab powup
    FindObjectOfType<HitFromBullet>().AddPowerUp(25); -- HitFromBullet is attach to enemy prefab
    }
    }


    HitFormBullet script:

    public float enemyLife = 100;
    public int _powUp = 25;

    void OnCollisionEnter2D(Collision2D coll)
    {

    if(coll.gameObject.name == "SHOOT(Clone)")
    {
    Destroy(coll.gameObject);
    print("gameobjectShooot" + " zmienna _powup " + _powUp); -- not work in this part _powUP = 25 and not increase :(
    enemyLife = enemyLife - (_powUp);
    if (enemyLife == 0)
    {
    Destroy(this.gameObject);
    FindObjectOfType<PlayerMove>().AddScore(100); -- this part work and increase score in playerMove script (main player script)
    }
    }
    }

    public void AddPowerUp(int powerUp)
    {
    _powUp = _powUp + powerUp; -- in this part _powUp increase
    print ("Dodałem 25 Zmienna powup: " + _powUp + "PowerUp : " + powerUp);
    }

    Thank you, hope some helps.

    Ps sorry for my bad bad english.
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    FindObjectOfType is not recommended, because it's slow. You should probably work out another way to determine damage.

    How many enemies do you have? This code would only change the damage done to a single object.
     
  3. Przemek_S

    Przemek_S

    Joined:
    Aug 18, 2014
    Posts:
    9
    i have many enemy object - 20 object or more. But _powUp in AddPowerUp increase and not increase in line life = life - _powUp. And I wondering why?
    For test I collect all powUp object and variables _powUp have 200 then I try to shoot enemy and I must firing 4 time not 1.
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Because AddPowerUp is only being called on a single enemy, and therefore only changing _powUp once, but you have 20+ enemies. So if there's 20 enemies, when you get the powerup, one enemy has +25 and nineteen enemies do not.

    You need to either change all the enemies (easiest way: just make _powUp a static variable), or just store the damage in the player and/or projectile objects.

    So, easiest fix:
    Change "public int _powUp = 25;" to "public static int _powUp = 25;"
    Change "public void AddPowerUp(int powerUp)" to "public static void AddPowerUp(int powerUp)"
    Change "FindObjectOfType<HitFromBullet>().AddPowerUp(25);" to "HitFromBullet.AddPowerUp(25);"
     
    Last edited: Sep 5, 2014
  5. Przemek_S

    Przemek_S

    Joined:
    Aug 18, 2014
    Posts:
    9
    Thank you for this solution. Is work great.